Re: 'new' keyword
- From: "James R. Twine" <spam@xxxxxxxxxxx>
- Date: 3 Aug 2005 04:38:02 -0700
"new" will not "partially allocate" memory and return a pointer to that
partially allocated memory; if it did, you would have no way of knowing
exactly how much was allocated.
"new" will throw an exception or return NULL when it cannot allocate
all required memory (what happens or which exception is thrown depends
on your environment and situation).
"new" will also "pass along" any exception that might be thrown by the
constructor of an object. So you can still get exceptions from the
point where you use "new" even though your environment may be
configured to have "new" return NULL if it fails to allocate.
In that case, any "fully constructed" member objects that were created
by the underlying object's constructor will be destructed, and the
memory allocated by "new" will be freed. The object's constructor will
not be called until the memory required by the object proper (meaning
"sizeof( TheObject )") has been allocated by "new" successfully. This
prevents you from getting a "partially constructed" object.
Note that the situation is very different when using "placement new",
because since you have to handle memory allocation and call
constructors (and destructors) explicitly, you have to perform
constuctor-based exception handling yourself. If you are not careful,
it is possible to end up with a partially constructed object in memory,
that may have been partially cleaned up, and you have to be careful not
to try to use it.
Peace!
-=- James.
.
- Follow-Ups:
- Re: 'new' keyword
- From: Alexander Grigoriev
- Re: 'new' keyword
- References:
- 'new' keyword
- From: Lisa Pearlson
- 'new' keyword
- Prev by Date: Re: My own mini MFC
- Next by Date: Re: Starting an action after a dialog has been drawn
- Previous by thread: Re: 'new' keyword
- Next by thread: Re: 'new' keyword
- Index(es):
Relevant Pages
|