Contributor
20Jun2006,19:44   #11
sharmila's Avatar
Hi shabbir,
Can you tell me how to check whether memory is allocated or not manually? and also about placement new operator?

Regards,
sharmila
Contributor
21Jun2006,03:16   #12
Aztec's Avatar
Quote:
Originally Posted by sharmila
Typically, array new will insert information adjacent to the memory allocated for an array that indicates not only the size of the block of storage but also the number of elements in the allocated array. This information is examined and acted upon by array delete when the array is deleted.
Yes, link is right to say that.

The allocation's size is kept somewhere. new [] probably stashes it just before or after the array. However, it's up to the implementation, so it can do whatever the hell it wants
Contributor
21Jun2006,03:19   #13
Aztec's Avatar
Quote:
Originally Posted by sharmila
When we allocate memory using new it will return the address if it is sucessful else null.
On failure new throws an exception bad_alloc() according to standard. Though some compiler doesn't conform to this and return NULL.

Last edited by Aztec; 21Jun2006 at 03:31..
Go4Expert Founder
21Jun2006,10:27   #14
shabbir's Avatar
Quote:
Originally Posted by sharmila
Can you tell me how to check whether memory is allocated or not manually?
By watching the memory in the memory watcher available with VC++ 6
Quote:
Originally Posted by sharmila
and also about placement new operator?
Here is an example
Code: CPP
#include <new>        // Must #include this to use "placement new"
 #include "Fred.h"     // Declaration of class Fred
 
 void someCode()
 {
   char memory[sizeof(Fred)];     // Line #1
   void* place = memory;          // Line #2
 
   Fred* f = new(place) Fred();   // Line #3 (see "DANGER" below)
   // The pointers f and place will be equal
 
   ...
 }
Line #1 creates an array of sizeof(Fred) bytes of memory, which is big enough to hold a object.
Line #2 creates a pointer place that points to the first byte of this memory.
Line #3 essentially just calls the constructor Fred::Fred(). The this pointer in the Fred constructor will be equal to place. The returned pointer f will therefore be equal to place.
Contributor
21Jun2006,11:44   #15
sharmila's Avatar
Thank you Aztec for responding.
Thank you Shabbir for placement new operator.Till now I did not know that we r having placement new operator also.
Thanks once again.

Regards,
sharmila.