Sign in to follow this  
jerrinx

Bizarre placement new[] operator return address

Recommended Posts

jerrinx    208

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();
}

Share this post


Link to post
Share on other sites
bubu LV    1436

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.

Edited by Martins Mozeiko

Share this post


Link to post
Share on other sites
jerrinx    208

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.

Share this post


Link to post
Share on other sites
jerrinx    208

Hey Phil,

 

Your damn right.

I could have just used placement on each element !!

I R IDIOT :)

 

Thanks for the link too !!

 

Regards,

Jerry

Share this post


Link to post
Share on other sites
Rattenhirn    3114

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.

Share this post


Link to post
Share on other sites
L. Spiro    25638

@L. Spiro.
A *pArr = new (pMem) A[4]; is not casting operation. It goes through operator new[] overloaded function.[/size]
It is used when you want to initialize an array of elements (calling constructors) given pre-allocated memory.[/size]

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

Share this post


Link to post
Share on other sites
Hodgman    51324
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.

Share this post


Link to post
Share on other sites
L. Spiro    25638

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

Share this post


Link to post
Share on other sites
SiCrane    11839
For those who care, the array allocation overhead is described as applying to placement new[] in section 5.3.4 paragraph 12 of the C++ standard (same location in all versions).

Share this post


Link to post
Share on other sites
Bregma    9214

Aside from trying to be Klever™ I suspect what the OP is doing is trying to reinvent either std::array or std::uninitialized_fill_n().  Perhaps he'd be more productive trying to use the language instead of modify it (although there's something to be said for discovering how your catalytic converter works instead of driving your car, too).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this