Bizarre placement new[] operator return address

Started by
10 comments, last by Bregma 11 years ago

Hey Guys,
I am trying to do in place initialization of an array of variables.
Essentially allocate memory. Pass in the void * into operator new[] to initialize an array of variables.
Now I get an address 4 bytes ahead of the allocated address !!
Example:
void *pMem = malloc(sizeof(A) * 4);
A *pArr = new (pMem) A[4];
pArr is (char *)pMem + 4 !!
Can anyone shed light on why ?
I noticed the first 4 bytes of pMem contains the number of elements in array. (in this case 4).
I could potentially move the pointer back and forth. But I want to know is this 4 Bytes same for any platform ? Is it same for 32 bit and 64 bit applications ??
Is this part of the C++ standard ?
Thanks !!
Jerry
/*********** Source Code *********/
class BaseString
{
enum EAllocationSource {AS_DATA, AS_HEAP};
EAllocationSource m_allocationSource;
union
{
const char *m_strStatic;
char *m_strDynamic;
};
unsigned int *m_pNumRef;
unsigned int m_len;
static const char m_nullString;
public:
virtual ~BaseString(){}
static void *operator new[] (size_t size, void *ptr)
{
printf("\nSize obtained in operator new[] function of class: %u", (unsigned int)size);
return ptr;
}
static void operator delete[] (void *val, void *ptr)
{
printf("\nAddress passed to delete in Class: %u", (unsigned int)val);
printf("\nIn Place Address passed in Class: %u", (unsigned int)ptr);
}
};
void main()
{
printf("\nSize to be allocated: %u", sizeof(BaseString) * 4);
void *pMem = malloc(sizeof(BaseString) * 4);
printf("\nAllocated Address: %u", (unsigned int)pMem);
BaseString *pArr = new (pMem) BaseString[4];
printf("\nReturned Address: %u", (unsigned int)pArr);
pArr->operator delete[](pArr, pMem);
printf("\nuint first: %u", *((unsigned int *)pMem));
while(true)
SwitchToThread();
}
JerrinX
Advertisement

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 restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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.

JerrinX

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

JerrinX

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.
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.

Erm, I know what placement new is and what it does.
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.

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! smile.png
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.

This is the correct answer in this case.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Huh, I've been C++ for over a decade, and am comfortable with dangerous constructs like placement new, but I've honestly never heard of placement new[]! Learn something new every day... From the sounds of some stack-overflow posts, it's not very useful though, and you should prefer the non-array one.

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This topic is closed to new replies.

Advertisement