"Neelesh Bodas" <ne***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
: Hi all,
: suppose the ctor of the class throws. Thus, at this point in time, some
: memory has been allocated but the object couldnot be constructed. Also,
: the memory is not yet freed. Does the c++ std automatically guarantee
: the call to operator delete?
Yes.
: TC++PL Edition 3, 14.4.5 says that the memory should be freed. But it
: doesnot say clearly whether it will be freed by calling operator
: delete() or something else.
Each new operator has a corresponding delete-operator, which is called
automatically when the construction of an allocated object fails.
In the standard, this is described in §5.3.4/17:
<<< If any part of the object initialization described above terminates by
throwing an exception and a suitable deallocation function can be found,
the deallocation function is called to free the memory in which the object
was being constructed, after which the exception continues to propagate in
the context of the new-expression. If no unambiguous matching
deallocation function can be found, propagating the exception does not
cause the object's memory to be freed. [Note: This is appropriate when
the called allocation function does not allocate memory; otherwise, it is
likely to result in a memory leak. ] >>>
This is actually the (only) purpose of placement-delete operators:
When defining a placement-new operator on your own, you must also
provide a matching placement-delete operator (with the same extra
parameters), which will be called to released allocated memory
in case a constructor throws.
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <>
http://www.brainbench.com