Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ISO C allow allocated memory to hang around after program termination?

An interesting point has arisen with some colleagues of mine, some of who claim that you should always free memory that you malloc no matter what. While I've always thought this is good practice in general, some others have contended that it is not necessary in a program like:

#include <stdio.h>
#include <stdlib.h>
int main (void) {
    char *mem = malloc (1000);
    if (mem != NULL) {
        // do something with mem
    }
    // memory not freed
    return 0;
}

Their claim was that the memory would be cleaned up when the process exits.

Now, being the local standard uber-geek, they approached me for clarification and, to my surprise, it seems the always-free crowd may actually be correct.

Turning to C11, 5.1.2.2.3 Program termination, it simply states that reaching the end of main is identical to calling exit.

7.22.4.4 The exit function lists those things that are cleaned up, specifically:

  • call all the atexit handlers.
  • all open streams with unwritten buffered are flushed.
  • all open streams are closed.
  • all files created by tmpfile are closed.
  • control is returned to the environment.

No mention is made in there of cleaning up the allocated memory.

Now looking at 6.2.4 Storage duration of objects, it mentions the four storage durations of which "allocated" is the one of interest here. It further states that:

Allocated storage is described in 7.22.3.

7.22.3 Memory management functions dictate the behaviour of all our favourites like malloc and free. At no point does it mention what happens to memory that hasn't been freed before the process terminates. It simply states:

The lifetime of an allocated object extends from the allocation until the deallocation.

Keep in mind this isn't a question about what implementations do - I'm well aware that just about every implementation I've ever seen stores its memory arena within the process space and that it's discarded when the process exits. This is what is allowed by the ISO C standard.

I cannot find anything in the standard that mandates this "free on terminate" behaviour, so an implementation where allocated memory survived process termination is feasible (think of a malloc that uses persistent shared memory for example).

So here's the question. Is it possible (according to ISO C) that allocated memory may continue to consume resources even after the process that allocated it has gone?

Or have I missed something in the standard that makes this moot?

like image 213
paxdiablo Avatar asked Feb 08 '13 02:02

paxdiablo


1 Answers

Clearing up allocated memory after program termination falls under the purview of the OS and not the C standard.

As far as I am aware, most major OSs do infact deallocate all allocated memory once the program is terminated. The exception might be embedded OSs, however I have no solid material yet to back this up.

Edit: Another thread that discusses the same issue - dynamically allocated memory after program termination

like image 76
Karthik T Avatar answered Sep 20 '22 22:09

Karthik T