1

Is there any mechanism like destructors in C? Or is there another way to achieve it?

My requirement is that when a program terminates all memory allocated at runtime should be freed. I keep a list of memory that is allocated using malloc.

Philip Conrad
  • 1,451
  • 1
  • 13
  • 22
Ravi
  • 1,574
  • 2
  • 16
  • 28

5 Answers5

2

Another alternative is to write a memory manager.

The idea is that the memory manager allocates large blocks of memory and divides it into smaller pieces for the rest of the program to use. When the program terminates, the memory manager can just delete the large blocks.

That's the basic idea, although the memory manager may need to be more complex depending on the memory usage profile of the program.

There's a basic memory manager in the Doom source code you could examine: http://doom.wikia.com/wiki/Zone_memory

MatthewD
  • 2,509
  • 2
  • 23
  • 27
1

A very simple solution is not to use malloc. This is an option adopted by some safety critical systems and they just use the stack.

Otherwise just terminate the program in a control fashion and tidy up during termination.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • This will work only if you don't need more than `ulimit -s` kilobytes of memory. – dreamlax Jun 25 '13 at 03:20
  • Just increase the limit. Or just malloc at the start the necessary memory and use the `atexit` function to free it. – Ed Heal Jun 25 '13 at 03:23
1

C++ uses RAII for managing resource lifetimes.
There is no such mechanism in C, Since you cannot have member functions for structures. Your main concern should be freeing memory allocations for reuse during the lifetime of the program rather than at the end of the lifetime. Once the program ends the OS will reclaim the leaked memory anyways.

Best way to do this in C is, to design your application to take care of lifetimes and code accordingly. This includes careful decision making of whether you really need dynamic memory allocations and if at all the lifetime of the allocated object should be well defined.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

The operating system will take care of this. When a program terminates, the OS will reclaim all memory used by the process.

Yuushi
  • 25,132
  • 7
  • 63
  • 81
  • 3
    The C standard doesn't guarantee that -- and some C code runs without an operating system. – Keith Thompson Jun 25 '13 at 03:19
  • Got it. Good discussion about same is [here](http://stackoverflow.com/questions/654754/what-really-happens-when-you-dont-free-after-malloc?rq=1) – Ravi Jun 25 '13 at 03:19
  • @KeithThompson Of course, the C standard can't guarantee that, because it has nothing to do with C specifically. If you aren't using an operating system, then you'll have to write your own version of `malloc`, and this of course changes the question significantly. Since `malloc` is referenced explicitly, I'm going to make the assumption there is an operating system around. – Yuushi Jun 25 '13 at 03:23
  • @Yuushi: It certainly could guarantee that `malloc`ed memory is deallocated on program exit. It just doesn't. `malloc` is a C-specific standard library function. The standard does guarantee that files are closed on program exit. – Keith Thompson Jun 25 '13 at 04:06
0

At application termination all memory allocated through malloc() will be freed unless the application becomes some kind of a zombie process. Normal termination should free all space otherwise.

The malloc() function uses operating system calls to allocate memory and when the process terminates the memory allocated to the process is reclaimed by the operating system.

I have seen cases of zombie processes under Windows in which a process stayed in memory hanging around until it was terminated via the Task Manager application.

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
  • Technically, a zombie process can't happen on Windows. A true [zombie process](http://en.wikipedia.org/wiki/Zombie_process) is one that *has terminated*, but no parent has `wait`ed on it (to read its exit status). What you're seeing on Windows is probably just a process that's waiting on some object in its exit path, which will never succeed. – Jonathon Reinhart Jun 25 '13 at 03:20
  • So this is an MSDN entry on zombie processes in Windows. http://msdn.microsoft.com/en-us/library/windows/hardware/ff566236(v=vs.85).aspx – Richard Chambers Jun 25 '13 at 03:23
  • I stand embarrassingly corrected. I'd never heard of that, and the first few search results were nothing "official" looking. I'll be sure to RTFM closer next time :-) – Jonathon Reinhart Jun 25 '13 at 03:26
  • 1
    Here is a something along the lines you describe in which a device driver was causing processes to hang. http://hardforum.com/showthread.php?t=1755879 – Richard Chambers Jun 25 '13 at 03:27
  • Yep, that sounds about right. Some processes blocked on `DeviceIoControl` can be nearly impossible to kill without a reboot. – Jonathon Reinhart Jun 25 '13 at 03:29