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

1

5

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?

Amethist answered 8/2, 2013 at 2:1 Comment(1)
All that you missed is that the concept of processes or anything that happens "after" process termination is completely outside the scope of the C standard, and thus of course the standard has nothing to say about it.Highlands
T
7

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

Therrien answered 8/2, 2013 at 2:3 Comment(4)
Ebmedded OS' would fall under the freestanding part of the standard where pretty much anything is allowed :-)Amethist
Indeed, as far as the C standard is concerned, the world ends when main returns or exit is called. There's no concept of processes or persistence of anything. Any real-world system that does have such concepts will define what, if any, resources persist after a process terminates, and memory obtained by malloc is never one that persists.Highlands
It's safe to say that in any system that supports separate virtual memory spaces per process, the process map will be destroyed when the project exits or is reaped and any pages assigned exclusively to it (whether obtained by malloc or any other way) will be returned to the system.Lumumba
On Atari TOS (I know it's old) some compilers would use the system Malloc function. There it could happen that allocated memory remained allocated after program run. It was annoying because there was also only a finite amount of allocations that could be made.Teenateenage

© 2022 - 2024 — McMap. All rights reserved.