2

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?

3
  • 2
    A bit of terminology: you declare and define names, like a here. You allocate objects on the heap. Well, formally, the "free store", but nobody calls it that. Commented Mar 12, 2013 at 17:14
  • duplicate of stackoverflow.com/questions/6727383/… Commented Mar 12, 2013 at 17:16
  • 2
    @PeteBecker: may I quote that in my user info, for reference when people do that "C++ has no heap so I refuse to answer your question" thing? Commented Mar 12, 2013 at 17:33

4 Answers 4

8

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

Normaly it will be cleard up by the OS.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.