new() without delete() is Undefined Behavior or merely Memory Leak? [duplicate]
[basic.life] (3.8 Object lifetime) in paragraph 4 tells :
A program may end the lifetime of any object by reusing the storage which the object occupies or by explicitly calling the destructor for an object of a class type with a non-trivial destructor. For an object of a class type with a non-trivial destructor, the program is not required to call the destructor explicitly before the storage which the object occupies is reused or released; however, if there is no explicit call to the destructor or if a delete-expression (5.3.5) is not used to release the storage, the destructor shall not be implicitly called and any program that depends on the side effects produced by the destructor has undefined behavior.
The standard is clear with regards to the semantics of new
and delete
. There's certainly no undefined behavior if you don't call delete
; it is, in fact, standard practice for singletons, and I imagine that std::cout
and std::cin
use new[]
to acquire their buffers (which they almost certainly never delete
). Why would not calling delete
be undefined behavior?
What is undefined behavior is calling the wrong form of delete
, calling free
for memory allocated with new
, or in general to attempt to delete an object without following the protocol required by its allocation.
Referring to [basic.stc.dynamic.deallocation] (aka 3.7.4.2 in n3337) there are only 4 paragraphs.
-
operator delete
andoperator delete[]
should be either class members or in global scope - Precisions on the valide signatures of
operator delete
andoperator delete[]
- Precisions on which
delete
can be used for deallocation, depending on whichnew
was used for allocation - Precisions on the possible arguments value and effects of the call (ie the pointers to this storage are now invalid)
There is absolutely no note here on what would happen if storage is allocated but never released.
I don't think that the Standard concerns itself with this, so it is more unspecified rather than undefined.