New () without delete () is Undefined Behavior or just a memory leak?

Possible duplicate:
Is memory leak problem an “undefined behavior” in C ++?

Never call delete or delete[] at the address returned by new or new [] resp in a C ++ program, is this Undefined behavior or just a memory leak?

References to the Standard are welcome (if any).
This happened in one of the comments here , and I'm a little confused about this.

+1
c ++ undefined-behavior new-operator memory-leaks
Mar 29 '12 at 7:35
source share
7 answers

[basic.life] (3.8 Object Lifetime) in paragraph 4 says:

A program can end the lifetime of any object by reusing the storage that the object occupies, or by explicitly calling the destructor for an object of the class type with a non-trivial destructor. For an object of class type with a nontrivial destructor, the program does not require a direct call to the destructor before the storage that the object occupies is reused or freed; however , if there is no explicit call to the destructor or if the delete expression (5.3.5) is not used to free the storage, the destructor should not be implicitly called, and any program that depends on side effects by the destructor has not determined the behavior.

+8
Mar 29 '12 at 8:00
source share

The standard is clear regarding the semantics of new and delete . Of course, there is no undefined behavior unless you call delete ; this is essentially standard practice for singles, and I believe that std::cout and std::cin use new[] to get their buffers (which they almost certainly never delete ). Why not call delete undefined behavior?

What is undefined behavior causes the delete form to delete wrong, calling free for the memory allocated with new , or even trying to delete an object without following the protocol required by its allocation.

+8
Mar 29 '12 at 7:41
source share

Referring to [basic.stc.dynamic.deallocation] (aka 3.7.4.2 in n3337), there are only 4 paragraphs.

  • operator delete and operator delete[] must be either members of the class or in the global scope
  • Valid signature accuracy operator delete and operator delete[]
  • The accuracy at which delete can be used to free, depending on which new used for placement
  • Evidence of the possible values ​​of the arguments and call effects (i.e. pointers to this store are now invalid)

There is absolutely nothing to say about what happens if the memory is allocated but not released.

I do not think that the standard refers to this, so it is more vague than undefined.

+1
Mar 29 '12 at 7:48
source share

This is just a memory leak.

But I clearly remember the standard saying that using new with delete[] and new [] with delete is undefined behavior. (or any combination with malloc or free )

I don't think the standard specifically says that calling new leads to undefined behavior unless you call delete . Also, how can runtime indicate if you call delete sometime later or never call it at all?

I don’t think there are any contracts in the standard that say - if you do X , you SHOULD make Y , otherwise it is UB.

0
Mar 29 '12 at 7:40
source share

Let's say that if you do not call delete, your program will still work. BUT, if you do not delete the memory allocation, your memory usage in the program will increase until your program runs out of free memory (the longer you use it, the higher the likelihood that it will happen), which will lead to failures in different points and it will be very difficult to detect (and I think Whats Undefined Behavior "mentioned in the comments)

0
Mar 29 '12 at 7:40
source share

I do not see how not freeing memory will lead to undefined behavior. If you do not clear, the OS still knows the information about the allocated memory. This will result in a resource leak while the application is running.

0
Mar 29 '12 at 7:41
source share

If delete/delete[] not called for objects allocated with new/new[] , there will be resource leaks. It could be a memory leak if the designer allocated dynamic memory. Other things, such as semaphore locking, are not released, files that are not released, etc., Can happen if the constructor selects them.

This will not be undefined behavior.

0
Mar 29 '12 at 8:12
source share



All Articles