25 April 2013

Simple valgrind tutorial


This is simple valgrind tutorial intended for begineers as well as professional c programmers. Ok lets get to code
consider this simple c program
[cpp]
int main()
{
char *t = (char *)malloc(sizeof(char)*100);
strcpy(t,"message is foo");
printf("%s",t);
return 0;
}
[/cpp]
everything is great here . You have used malloc to allocate memory dynamically and then you have copied some message into that memory and then you have printed that message on screen and then you exited successfully.
there are no errors with compiler or  not even a single warning. SO what possibly could be wrong with this ?
But there is a glitch in this code and believe me this is a very big programming bug once your code size increases.
The problem is you have forgotten to free the memory which is allocated to p. This is not a problem for a small program like this but imagine if you forget to release memory for every allocated memory in a program of thousands of line , that process will consume all the available memory of your system .
Repeated memory leaks cause the memory usage of a process to grow without bound.
This concept is knows as memory leak . you allocate memory and forget to deallocate it .Ideally your program  should free every byte of heap memory before your program exits.
for this you have to use software tools
for linux we have valgrind
for windows i don't know
valgrind is basically a collection of tools . For our purpose we are using a tool knows as memcheck.
[cpp]
//test_val.c
int main(int argc,char *argv[])
{
int *i;
int *j;
i = (int *)malloc(sizeof(int));  //Allocating dynamic memory
j = (int *)malloc(sizeof(int));  //Allocating dynamic memory
printf("\nenter value for i :\n");
scanf("%d",i);
printf("\nEnter the value for :\n");
scanf("%d",j);
return 0;
}
[/cpp]
first compile this program with -g option
[cpp]
gcc -g -o test_val test_val.c
[/cpp]
next run the valgrind tool
[cpp]
valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./test_val
[/cpp]
If --leak-check=full is specified, Memcheck will give details for each definitely lost or possibly lost block, including where it was allocated.
on screen you will see something like this
image
Here 8 byte memory is definitely lost as we allocated memory for variable i and j but we haven’t freed them. To recover this memory leak we need to free the memory allocated for the variables using free.
[cpp]
//test_val.c
int main(int argc,char *argv[])
{
int *i;
int *j;
i = (int *)malloc(sizeof(int)); //Allocating dynamic memory
j = (int *)malloc(sizeof(int)); //Allocating dynamic memory
printf("\nenter value for i :\n");
scanf("%d",i);
printf("\nEnter the value for :\n");
scanf("%d",j);
free(i);
free(j);
return 0;
}
[/cpp]
img
now consider below code
[cpp]
//test_val.c
int main(int argc,char *argv[])
{
int *i;
int *j;
int x =10;
i = (int *)malloc(sizeof(int)); //Allocating dynamic memory
j = (int *)malloc(sizeof(int)); //Allocating dynamic memory
printf("\nenter value for i :\n");
scanf("%d",i);
printf("\nEnter the value for :\n");
scanf("%d",j);
j = &x;
free(i);
free(j);
return 0;
}
[/cpp]
now re run valgrind .You will see the something like in the below image
image
The reason behind that, it is giving 4 byte memory leak in leak summary is, before freeing the memory pointed to by j, j is updated to point to another memory block. That’s why the previously allocated memory is definitely lost.


SHARE THIS

Author:

1 comment:

  1. Thank you a lot for sharing this with all of us you actually know what you are speaking approximately!
    Bookmarked. Please additionally talk over with my site =).
    We will have a hyperlink exchange arrangement between us

    Also visit my web blog ... increase youtube views

    ReplyDelete