14.19 – Memory leakage and NULL pointer

by subbu on February 3, 2014

Memory leakage:

What happens when some memory is allocated in the heap using dynamic memory allocation, but has not de-allocated?

The result would be that the heap gradually fills up as there would be continuous request to allocate, but no de-allocation requests to return free memory to re-use.

A program which forgets to de-allocate dynamically allocated memory is said to have a “memory leak”.

It may not be a serious problem for a program runs for a short time because it executes and exits but, is a big concern for the programs run for long time. The memory leak gradually fills up the heap and there would be a time where program could not get free heap. It may result slow in running or some times system may crash.

Possibilities of memory leak:

Case 1:

We know that a pointer is stored in the stack segment and dynamically allocated memory is allocated in the heap segment.

Pointer has a scope like any other variable but, dynamically allocated memory has no scope and only de-allocated on completion of program execution.

void allocate(int n)
{
 int *p;
 p=(int*)malloc(sizeof(int)*n);

}

In the above function malloc() allocates the memory in the heap, p is the pointer to heap allocation. The scope of pointer ends on completion of function execution but, heap allocation would not be de-allocated which results memory leakage.

Case 2:

It may result memory leak when allocates for multiple times and points with the same pointer.

int *p;
p=(int*)malloc(10);

p=(int*)malloc(20);

The address returned from the second allocation overwrites the address of the first allocation. Consequently, the first allocation becomes a memory leak.

Case 3:

Some times copying pointers may leads to memory leakage.

int *p,*q;
p=(int*)malloc(10);
q=(int*)malloc(10);

p=q;

Both p and q points different memory locations, address of second allocation is copied into p. Now both p and q points the second allocation. Consequently, first allocation becomes a memory leak.

Conclusion:

The best way to get rid from this problem is de-allocating allocated memory using free() when we don’t need any more.

NULL value:

NULL is a macro defined in header file “stdio.h” as

#define NULL 0

The preprocessor would replace 0 in place of NULL within the program.

#include<stdio.h>
int main()
{
 #ifdef NULL
  printf("NULL is defined");
 #endif
return 0;
}

Output:
NULL is defined

It is proving that NULL is a macro which is already defined

#include<stdio.h>
int main()
{
 int x=NULL;
 printf("%d",x);
 return 0;
}

Output:
0

It is proving that the replacement value of NULL is 0

#include<stdio.h>
int main()
{
 if(NULL==0&&NULL=='\0')
   printf("All are equal");
 else
   printf("All are different");
return 0;
}

Output:
All are equal

The ASCII value of a back slash character ‘\0’ is zero. It is proving that 0, ‘\0’ and NULL are same.

NULL pointer in C language

NULL pointer is a pointer which is set to address 0. The actual meaning of null pointer is that it is not pointing to anything. It is practically used when dealing with dynamic memory allocation. A null pointer generally expresses no memory has been allocated at. It helps to conditionally allocate the memory

short *p=NULL;
if(!p)
  p=(short*)malloc(10);

The above code allocates the memory when no memory has been allocated. Here NULL pointer behaves like a logical NOT

It is always best practice to assign NULL value to the pointers just declared because like normal variables even pointers are undefined on declaration

short *p=NULL;

or

short *p=0;

Similarly, when the dynamically allocated memory is de-allocated using free(), the pointer points to it must be assigned with NULL

short *p=NULL;

if(!p)
  p=(short*)malloc(10);
free(p);

/* using the memory here */
if(p)
  *p=45;

Because p has not been set to NULL, the if condition evaluates to true, and the program tries to assign 45 to de-allocated memory. It may result program to crash. It is not a good idea to leave a pointer pointing to de-allocated memory.

When de-allocating the memory, set the pointer to NULL, immediately after de-allocated. This helps to ensure the program does not try and access memory that has already been de-allocated. The above code must be written as

short *p=NULL;

if(!p)
  p=(short*)malloc(10);
free(p);
p=NULL;

/* using the memory here */
if(p)
  *p=45;

Conclusion:

Make the habit of assigning a pointer with NULL both when the pointer is declared and de-allocated

#include<stdio.h>
int main()
{
short *p=NULL;
if(!p)
  p=(short*)malloc(sizeof(short));
*p=45;
printf("%d",*p);
free(p);
p=NULL:
return 0;
}

It is the best way of writing program

What would be the output of following program?

#include<stdio.h>
int main()
{
short *p;
printf("%u",p);
return 0;
}
Show Answer
Output in Turbo C:
0

Output in gcc:
Garbage value

Unassigned pointer is generally assigned with garbage value like any normal variable

What would be the output of following program?

#include<stdio.h>
int main()
{
short *p=NULL;
*p=45;
printf("%d",*p);
return 0;
}
Show Answer
Output in Turbo C:
45

Output in gcc:
Segmentation fault

NULL pointer can’t be dereferencing without allocating the memory

Previous post:

Next post: