Jump to content

  • Log In with Google      Sign In   
  • Create Account


Bizarre placement new[] operator return address


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 jerrinx   Members   -  Reputation: 208

Like
0Likes
Like

Posted 11 April 2013 - 09:31 PM


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

Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 12249

Like
-2Likes
Like

Posted 11 April 2013 - 11:02 PM

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


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#3 Martins Mozeiko   Crossbones+   -  Reputation: 1413

Like
3Likes
Like

Posted 11 April 2013 - 11:16 PM

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, 11 April 2013 - 11:17 PM.


#4 jerrinx   Members   -  Reputation: 208

Like
0Likes
Like

Posted 11 April 2013 - 11:43 PM

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

#5 phil_t   Crossbones+   -  Reputation: 3203

Like
3Likes
Like

Posted 12 April 2013 - 12:08 AM

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?



#6 jerrinx   Members   -  Reputation: 208

Like
0Likes
Like

Posted 12 April 2013 - 12:34 AM

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

#7 Rattenhirn   Crossbones+   -  Reputation: 1661

Like
2Likes
Like

Posted 12 April 2013 - 01:28 AM

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.



#8 L. Spiro   Crossbones+   -  Reputation: 12249

Like
-1Likes
Like

Posted 12 April 2013 - 03:55 AM

@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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#9 Hodgman   Moderators   -  Reputation: 27677

Like
0Likes
Like

Posted 12 April 2013 - 05:54 AM

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.

#10 L. Spiro   Crossbones+   -  Reputation: 12249

Like
0Likes
Like

Posted 12 April 2013 - 06:40 AM

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


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#11 SiCrane   Moderators   -  Reputation: 9392

Like
1Likes
Like

Posted 12 April 2013 - 08:11 AM

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

#12 Bregma   Crossbones+   -  Reputation: 4765

Like
0Likes
Like

Posted 12 April 2013 - 09:31 AM

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


Stephen M. Webb
Professional Free Software Developer




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS