cMemDbg – Easy to use C memory leak detection library

After some time working with C, I decided to develop a very easy to use library which can help you to detect and track memory leaks.

There are lots of solutions for this on the net, but this one has the particularity of being really simple to implement.

You just have to add the following include to your main include file (a file that gets included by each file of your project) or, in case you don’t have one, to each file that calls any memory allocation function (malloc, realloc, calloc or free). The only extra caution you have to take into account is to include it after the standard headers (stdio.h, stdlib.h, malloc.h, etc.).

This is the line:

#include "cMemDbg.h"

That’s all… Expecting more? Well, there is a final step… You have to make this call just before you exit your program so you can get the detailed conclusions:


Easy, not?

That was all… Really.

Now, before going to more “expert” features, I’ll leave here the link to download the library for the ones who don’t wish to keep reading:

Now let’s continue…

The library comes with three configurable settings (available in cMemDbg.c):

  • PRINT_OPERATIONS: If set to 1, it will print to the configured output (stdout by default) each memory alloc operation done in the program (allocation or free). Otherwise, the library will just print problems and notifications and the final dump. [Default value = 1]
  • MAX_ALLOC: Internal library memory stack length (in elements). It is the max amount of allocations that can be tracked without being freed. You can increase this value at will if needed. In fact, if it is needed, the library will print a message saying: “INTERNAL_ERROR: Allocation stack overflow, please increase MAX_ALLOC”. [Default value = 256]
  • PRINT_OUTPUT: Location to print the library generated notifications (can be any stream/file in which you could print using fprintf). [Default value = stdout]

Finally, there are two more functions you could use wherever needed:

  • PrintTotalAllocatedMemory(): Prints the accumulative amount of memory allocated at the moment of the call.
  • PrintMemoryReservedByCMemDbgLibrary(): Prints the amount of memory reserved by the library (defined at compilation time by the MAX_ALLOC constant).

That’s all… I said it was easy to use.

Anyway, if after reading the cMemDbg.h files’ comments, you have any questions, comments or suggestions, please feel free to contact me.

Support appreciated!

All the content offered in this website is, except noted otherwise, of free nature. This means you can share it wherever you want if you do it freely and stating its source.

If it was useful for you and you’d like to contribute, you can make a donation or, at least, visit one of our advertisers of your choice; they are all around the site.

Incoming search terms for the article:

7 Responses to “cMemDbg – Easy to use C memory leak detection library”

  • The article is ver good. Write please more

  • Hi. I’m testing your library and it seems to be working very well.

    I program in C for my company’s FreeBSD servers and would like to suggest a patch for your library to support FreeBSD systems due to libc differences.

    Nice work!


    File: freebsd.patch

    — cMemDbg.c 2009-11-09 19:56:45.000000000 -0200
    +++ cMemDbg.c.dist 2009-04-08 15:45:22.000000000 -0300
    @@ -33,10 +33,7 @@

    -// FreeBSD has malloc inside stdlib
    -#ifndef __FreeBSD__
    -#endif /*__FreeBSD__*/

    //Configuration Constants ***************
    #define PRINT_OPERATIONS 1

    • The comment system stripped the includes. LOL. If you want me to send the patch to you by mail, just let-me known.


      • Thank you Sérgio for your comment!

        I’m very happy this library was useful for you…

        Please send me the diff via the Contact section so your code doesn’t get stripped :)

        I’ll include your header changes in the following release…

        BTW, if you work with C++ code, you can use the cppMemDbg version…

        Thanks again!


  • It’s a very interesting library but, what should I modify to get a memory leak log file like, for example, “leaks_log.txt”? I have tried to replace the line:
    #define PRINT_OUTPUT stdout
    by a (FILE*) but the compiler does not accept it.

    • Hi! Thanks for your comment!

      stdout is not a FILE* but a file descriptor integer (the ones returned by the “open” function). So you should replace it with other of the same kind.

      You can get a file descriptor from a FILE* using the function “int fileno(FILE *stream)”.

      Given a global FILE* f, you could replace the line this way and it should work:

      #define PRINT_OUTPUT (fileno(f))

      Best regards!


Leave a Reply to NeoEGM