Bizarre placement new[] operator return address
Because A is a class, casting from void * to A * may involve shifting of the address, especially if it inherits from another class.
L. Spiro
I don't think that casting from void* to A* will change address. Cating from B* to A* - yes it can, but not from void*.
I think in this case author is allocating memory with new [] and array allocation stores length in first 4 bytes, that's why it returns address with +4 offset.
Hey guys,
Thanks for your replies.
@L. Spiro.
A *pArr = new (pMem) A[4]; is not casting operation. It goes through operator new[] overloaded function.
It is used when you want to initialize an array of elements (calling constructors) given pre-allocated memory.
@Martins
I agree that void* to A* will not change address.
If you notice the operator new[] is not changing the address. It is returned as such. Also in delete[] the value obtained is -4 of the address passed in (In other words the original address allocated). So the new[] operator is doing a +4 and delete is doing a -4. C++ is doing it !!
Try executing the code on your computer.
Also an update on my find:
I've tried the above code on a variety of different configurations. On Windows, On Unix and on A 64 Bit Application.
Here are my observations, when using In Place new[] operator overloading:
1. The memory returned from operator new[] is shifted forward by sizeof(size_t).
2. The memory passed as argument to delete[] is shifted back by sizeof(size_t).
3. The size argument to operator new [] will be equal to (sizeof(class) * numofArray) + sizeof(size_t).
4. The first size_t bytes of memory returned from new[] contains the number of elements allocated.
Also please note the above code needs to be changed to allocate +sizeof(size_t) memory to function without memory problems.
void *pMem = malloc(sizeof(BaseString) * 4 + sizeof(size_t));
I am wondering whether this is a C++ standard. If it is, I'd tweak the addresses myself and use it.
Some discussion on this here:
http://stackoverflow.com/questions/4011577/placement-new-array-alignment
Is there any reason you can just placement new these A's individually?
Hey Phil,
Your damn right.
I could have just used placement on each element !!
I R IDIOT :)
Thanks for the link too !!
Regards,
Jerry
When new-ing an array of non-PODs, the compiler needs to store how many elements are in that array, so that it know how many destructors to call on delete. This value is often stored in memory before the array. By extension, this also means that it will allocate a bit more memory than N times sizeof(element), so your original placement new might write over the end of your buffer.
This behaviour is compiler specific, the best advice is to stay away from placement new for non-pod arrays! :)
If you can't do that, you may need to adapt your implementation for each compiler, so it might be easier to use a vector with a custom allocator.
Erm, I know what placement new is and what it does.@L. Spiro.
A *pArr = new (pMem) A[4]; is not casting operation. It goes through operator new[] overloaded function.
It is used when you want to initialize an array of elements (calling constructors) given pre-allocated memory.
And yes, it is implicitly performing a cast. You passed a void * and got back an A *. The address you passed was cast to A *. Shifting of addresses can happen in these cases, as I mentioned, specifically if A inherits from another class.
This is the correct answer in this case.When new-ing an array of non-PODs, the compiler needs to store how many elements are in that array, so that it know how many destructors to call on delete. This value is often stored in memory before the array. By extension, this also means that it will allocate a bit more memory than N times sizeof(element), so your original placement new might write over the end of your buffer.
This behaviour is compiler specific, the best advice is to stay away from placement new for non-pod arrays!
If you can't do that, you may need to adapt your implementation for each compiler, so it might be easier to use a vector with a custom allocator.
L. Spiro
Actually me too.
I have never even thought of using placement new with an array. Never seen it before, never considered using it, never considered that it existed.
L. Spiro