I use Dev C++ version 5.11 and Windows 8.1 operating system, my question is for a program that I develop in C, how can I verify that the program is free of memory leaks? Thank you
I use Dev C++ version 5.11 and Windows 8.1 operating system, my question is for a program that I develop in C, how can I verify that the program is free of memory leaks? Thank you
NOTE
In the comments to the answer, @JacoboCórdova indicates that some modifications are necessary to make it work.
I guess they are due to the different compilers used.
I include said modification, conveniently marked in
leaks.c
. In case of problems, DELETE said change.END OF NOTE
There is a relatively simple way:
You have to create 2 additional files to your project: one header
and another with some code. Note that, in said file, we do not include the previous one
leaks.h
:Now, to use the new feature, you have to include
"leaks.h"
at the beginning of all the files.c
you want to check . Specifically, it must be the last#include ""
one you have.And one last modification. In the place where you want to know how you carry the number of leaks , for example in
main.c
:As you can see, the idea is to replace normal function calls with calls to our replacement functions, and do what we need to do there. It's no wonder, but it can give us an idea of how we handle memory leaks.
Note that we do not replace
realloc( )
. I leave it as entertainment ;-)NOTE
This proposed implementation only counts the number of times we call
malloc( )
yfree( )
in our source files . That implies that it is not, far from it, an exhaustive control of what we do.To obtain more accurate data , we have several options:
my_malloc( )
,my_calloc( )
, ... Right now, we hardly do anything. But nothing prevents us from increasing our functionality, returning different things, counting the exact bytesrealloc( )
we are using, contemplating , creating linked list nodes of the blocks, even including the name of the exact file we fall back on (using__FILE__
), .. .EDIT
It is very important that we understand one thing: it is possible to have memory leaks even if we do the correct
alloc( )
andfree( )
.We're already into low-level stuff , but I think it's important to know.
The memory spare/free functions are not miraculous ; they are simple functions, and can add things to what we ask for .
Most actually add things before the address they return to us .
By this, I mean that if we mistakenly access pointers returned by
malloc( )
and the like, we can corrupt internal system memory data. As a consequence, we can make correct use of these functions, and, even so, leave gaps in memory that cannot be recovered (until we close the program).A small example:
The code above accesses a zone just before what is returned to us
malloc( )
.There, several things can happen:
malloc( )
previous one.free( )
(management system noticed).At this last point, literally anything can happen . Among other things, we can leave a huge hole in memory that we can never use .
C is famous and feared for the ease it offers to manipulate memory at will . There we have a possible reason for the second. Be very careful what we do with our pointers!