|
-
January 4th, 2009, 11:59 AM
#26
Re: Heap efficiency?
 Originally Posted by laserlight
Yes, but how does that conflict with what I stated? My understanding is that placement new is used in the standard allocator's construct member function.
Internally, it may be... creating an object involves two steps:
1) "Selecting" (for lack of a better word) a memory address or contigous block for items with sizeof()>1. This can be on the stack, dynamically aquired from the allocator (by default on the general heap), or specified by the caller.
2) Invoking the constructor, passing the effective base of this memory as the this pointer.
Of the three:
1) Memory on the stack
2) Memory allocated at the time of the call
3) Memory with the address specified.
Only the second one is relevant to any discussion about performance (how long it takes and how much memory is consumed). Condition #1 is a fixed compile time offset from the stack pointer, and Condition #3 is a parameter passed to a routine.
In psuedo code, this may be:
Code:
*Object New()
{
*Memory = AllocateMemory();
*Object = InvokeConstructor(*Memory);
}
*Object PlacementNew(*Memory )
{
*Object = InvokeConstructor(*Memory);
}
Or may be:
Code:
*Object New()
{
*Memory = AllocateMemory();
*Object = PlacementNew(*Memory);
}
*Object PlacementNew(*Memory )
{
*Object = InvokeConstructor(*Memory);
}
The latter is likely to be a common implementation. But the impact of replacing the allocator (either on a per-class basis, or globally) is to replace the psuedo-"AllocateMemory()" functionallity.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|