siloan
siloan

Reputation: 89

c++ heap memory allocation/deallocation

If i declare something on the heap like char *a=new char[1000] and the main program stops what will happen with that allocated memory if there is no delete[] call? It remains on the heap or is automatic deallocated?

Upvotes: 2

Views: 1542

Answers (4)

Mats Petersson
Mats Petersson

Reputation: 129374

What the C++ standard specifies "ends" shortly after you return from main() - it does explain that global objects are destroyed at some point after this, atexit() and some other "we're quitting" level functions also get run after main returns. But what happens to the memory that your program lives in is not specified by the C++ standard. The same applies to the contents of the heap.

It is up to the OS to clear up the application, if there is an OS in the system (C++ doesn't specify that you have to have an OS either).

Upvotes: 2

Subhajit
Subhajit

Reputation: 320

When your application crashes or gets aborted for whatever reason, the OS reclaims the memory in normal case. But,this is undefined.

Upvotes: 0

marscode
marscode

Reputation: 373

Normaly it will be cleard up by the OS.

Upvotes: 0

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

As far as C++ is concerned, what will happen to it is totally undefined. However, pretty much any reasonable operating system will clean up the memory allocated by a process when it has terminated. It is, however, a very good practice to clean up after yourself.

Upvotes: 8

Related Questions