valggrind
Memory Leak :
A memory leak or leakage in “C Programming” is nothing but a programrammer has dynamically allocated a memory for its use and during the exit of the program, programmer unable/failed to release memory he has acquired.
Is this a hazard ?

Yes this a hazard ! Then the next question arises is how it is hazardous.

All the memory that are allocated during dynamic programming is allocated in Heap ( nothing but in RAM ). So eventually when we reboot the system / restarted the system all the memory allocated are released automatically. Assume that in a mobile(which is not rebooted often) a program has allocated memory and it has not freed . What happens is eventually when the program is invoked repeatedly the memory is not freed in the RAM and the mobile is sluggish in its behaviour starving for RAM, Once we reboot the mobile , Mobile is as fast as expected.
How to identify Memory Leak?
Eventually the programmer has to take care about memory leak.
In programming concert there are several ways to solve this issue.
1. Count Method
1. Count method is nothing but whenever a memory is allocated count it.
2. Whenever a free is occurred reduce the No of Counts.
3. If the count is zero at the end of program , then there is no memory leak’s available.
2. Valgrind

This sample program explains how valgrind can be used to identify memory leaks.
In this sample program a, b are allocated 80 bytes of memory totally
This example shows how valgrind identifies when there is no memory leak and how the output is different when there is memory leak.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main()
{
	char *a;
	char *b;
 
	a = (char *) malloc(sizeof(char) * 40);
	b = (char *) malloc(sizeof(char) * 40);
 
 
	strcpy(a,"Memory Leak Example");
	strcpy(b,"Sample Program");
	printf(" %s",a);
	printf("\n %s\n",b);
 
	free(a);
	free(b);
}

Memleak2

Memleak3

Memleak4

Memleak5

Share

Related posts:

  1. Segmentation faults cause and avoidance
  2. gdb – examine memory (using x command)
  3. The main function
  4. PulseAudio introduction and Architecture
  5. Samsungs first android mobile i7500