top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a function to check memory leak in a program using c ?

0 votes
486 views
Write a function to check memory leak in a program using c ?
posted Dec 6, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

To check the memory leak we can use Valgrind tool

For linux users

go to root and
apt-get install valgrind
and check memory leak using command
**valgrind --leak-check='yes' "./executable file name" **

For example

#include <stdlib.h>
int main()
{
    char *x = malloc(100); /* or, in C++, "char *x = new char[100] */
    return 0;
}

EXECUTE:

% valgrind --tool=memcheck --leak-check=yes example1


This will result in some information about the program showing up, culminating in a list of calls to malloc that did not have subsequent calls to free:

==2116== 100 bytes in 1 blocks are definitely lost in loss record 1 of 1
==2116== at 0x1B900DD0: malloc (vg_replace_malloc.c:131)
==2116== by 0x804840F: main (in /home/cprogram/example1)

answer Dec 12, 2016 by Mohan Raj N
...