Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ is it necessary to delete dynamically allocated objects at the end of the main scope?

When using dynamically allocated objects in C++ eg:

TGraph* A = new TGraph(...);

One should always delete these because otherwise the objects might still be in memory when control is handed back to the parent scope. While I can see why this is true for subscopes and subroutines of a program, does the same count for the main scope?

Am I obliged to delete objects that were dynamically built inside main()? The reason why this seems a bit redudant to me is that when main ends, the program also ends, so there is no need to worry about memory leaks.

like image 989
romeovs Avatar asked Apr 23 '12 09:04

romeovs


People also ask

What happens if you don't delete dynamically allocated memory?

If you don't delete them, they will remain there (but won't be accessible -> this is called memory leak) until your process exits, when the operating system will deallocate them. Save this answer.

When should dynamically allocated memory be deleted?

For dynamically allocated memory like “int *p = new int[10]”, it is the programmer's responsibility to deallocate memory when no longer needed. If the programmer doesn't deallocate memory, it causes a memory leak (memory is not deallocated until the program terminates).

Why do we need to call delete for dynamically allocated variables?

The C++ keyword delete is used to release dynamically allocated memory so that it may be reused. Dynamic variables (memory allocated by newshould be deleted when they will no longer be used. Deleted dynamic variables should NOT be reused unless that have been reallocated with another call tonew.

Which function deletes the dynamically allocated memory?

It is the programmer's job to deallocate dynamically created space. To de-allocate dynamic memory, we use the delete operator.


2 Answers

Most of the modern OS always reclaim back all memory they allocated to a program(process).
The OS doesn't really understand if your program leaked memory it merely takes back what it allocatted.

But there are bigger issues at hand than just the memory loss:

Note that if the destructor of the object whos delete needs to be called performs some non-trivial operation and your program depends on the side effects produced by it then your program falls prey to Undefined Behavior[Ref 1]. Once that happens all bets are off and your program may show any beahvior.

Also, An OS usually reclaims the allocated memory but not the other resources, So you might leak those resources indirectly. This may include operations dealing with file descriptors or state of the program itself etc.

Hence, it is a good practice to always deallocate all your allocations by calling delete or delete [] before exiting your program.


[Ref 1]C++03 Standard 3.8 Para 4:

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

like image 175
Alok Save Avatar answered Nov 16 '22 00:11

Alok Save


IMO it is best to always call delete properly:

  • to make it an automatic habit, making it less likely to forget it when it is really needed
  • to cover cases when non-memory resources (sockets, file handles, ...) need to be freed - these aren't automatically freed by the OS
  • to cater for future refactoring when the code in question might be moved out of main scope
like image 44
Péter Török Avatar answered Nov 16 '22 01:11

Péter Török