Re: 'new' keyword

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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

.



Relevant Pages

  • Re: Technical two dimensional array question
    ... > Does it allocate a contiguous blocks of memory or not? ... What it allocates is memory space for *references* to string objects, ... > I tried it and I get an exception error, as I should, but, ... allocated array. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: new keyword
    ... Call constructor for object placed into allocated memory ... If an exception is thrown from the constructor, handle it, cleanup ... re-throw the exception up the stack. ... instantiate one in an available subblock. ...
    (microsoft.public.vc.mfc)
  • Re: Non-failure guarantied malloc/new
    ... >> error code to the caller to inform that something went wrong. ... After all I can recieve such a exception at any time so I will have no ... > You could allocate all needed memory in static blocks. ...
    (comp.lang.cpp)
  • Re: Allocators and exceptions
    ... of an object never propagates an exception, ... I guess I had misunderstood what's meant by 'constructor', ... the leak in this case doesn't bother me too much, ... very long-running applications are likely to have problems with memory ...
    (comp.lang.ada)
  • Re: Help with classess, access violation
    ... when memory is allocated for an object. ... to be created on the heap, and that also causes the constructor to run. ... In Delphi, you can only allocate a new object on the heap, and you do ... so by calling a constructor on a class reference. ...
    (alt.comp.lang.borland-delphi)