Reputation: 89
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
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
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
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