Search code examples
cmemory-managementmallocfree

How can I do automatic memory management in C?


In C memory allocation/deallocation done by malloc and free.

In C++ memory allocation/deallocation done by new and delete.

There are some solutions in C++ for automatic memory management like:

  • Smart Pointers.
  • RAII (Resource Acquisition Is Initialization)
  • Reference counting and cyclic references
  • ...

But how can I do automatic memory management in C?

Is there any solutions for AUTOMATIC memory management in C?

Is there any guidelines or something like that for C?

I want when I foget free a block of memory:

  • My code doesn't compile

-- or --

  • Memory automatically deallocated

And then I say: Oh, C is better than C++, Java and C#. :-)


Solution

  • You may use a Boehm garbage collector library.