C - SPLessons

Chapter 34

Memory Management in C

Memory Management in C

Memory is nothing but storage area of data. Using the available memory efficiently can be referred as “Memory Management” in C. Memory can be basically divided into 4 segments:

1.Code segment: Code segment consists of binary code which has to be executed.

2.Data segment: Data segment can be further divided into two types.

(i) Initialized data segment: The data which is already declared like global, constant and static data is stored in this segment.

(ii) Uninitialized data segment: The data which is not initialized like uninitialized global variables are stored in this segment.

3.Heap: Heap consists of programs which are allocated memory at run-time. The heap always grows upward.

4.Stack: In stack segment, local variables are stored and that can be used to pass the arguments to the functions when required. It also sends the address to get back to the point, after the function call is over. The stack is added downwards.

Memory is allocated to various data in two ways :

Static Memory Allocation

Static memory allocation basically means, memory is allocated at compile-time. It does not change at run-time.

For example, while using arrays, the size is specified at the beginning itself. If considered pointers, compiler never allocate memory for pointers.

Sometimes, there will be wastage of memory as C does not have automatic garbage collector.In order to overcome, we go for dynamic memory allocation.

Dynamic Memory allocation

The process of allocating memory at run-time is called as Dynamic Memory Management in C. It allows the user to request the memory space and enter the data at the time of execution. Dynamic memory allocation will occupy the heap of memory and frees the storage when task is completed.

Memory allocation in C-language is done by using malloc() and calloc() functions.

malloc function occupies a continuous block of memory of given size and returns a pointer of type “void” which means pointer can be of any data type. If the requested memory is not provided then malloc() returns NULL.
ptr =(cast_type*) malloc(size)

Here, ptr is pointer of cast type.

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int i,length,sum=0;
	printf(" Enter the count of numbers:");
	scanf("%d", &length);
	int *p;
	p=(int*) malloc((sizeof(int)*length);
	for(i=0; i<length;i++)
	{
		printf("Enter a number :");
		scanf("%d",p+i);
	}
	for(i=0; i<length;i++)
	{
		sum=sum+*(p+i);
	}
	return 0;
}

After using malloc() function memory must be restored using de-allocation.

De-allocation of memory:

The memory has to be de-allocated to efficiently use the memory space. So, to free up the memory allocated, use free() function. Then that freed space is ready to use again.

ptr= void free(void *ptr);

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int i,length,sum=0;
	printf(" Enter the count of numbers:");
	scanf("%d", &length);
	int *p;
	p=(int*) malloc((sizeof(int)*length);
	for(i=0; i<length;i++)
	{
		printf("Enter a number :");
		scanf("%d",p+i);
	}
	for(i=0; i<length;i++)
	{
		sum=sum+*(p+i);
	}
	return 0;
}

use free(p); in the above program before return 0;

calloc() will function in an advanced way by tracking the address of the memory. This can be used for dynamic allocation of arrays.
ptr =(cast_type*) calloc(count,size);

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
	int length,i;
	int *ptr;
	printf("Enter count of number:");
	scanf("%d",&length);
	ptr=(int*)calloc(length,sizeof(int));
	if(ptr==NULL)
	{
		printf("Cannot allocate memory");
		exit(1);
	}
	for(i=0;i<length;i++)
	{
		printf("Enter a number %d: ",i);
		scanf("%d",&ptr[i]);
	}
	for(i=0;i<length;i++)
	{
		printf("Enter a number %d: ",i);
		scanf("%d",ptr[i]);
	}
	return 0;
}

Reallocation of memory

Reallocation means if the allocated memory is not sufficient (or) to delete the extra memory at run-time, there is a chance to increase or decrease the memory using realloc() function.

It takes two parameters,

  1. pointer of previous allocated memory from malloc(),calloc() or realloc() and
  2. size of new memory created.
ptr= (void*)realloc(void  *ptr, size);

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int a[5],i;
	int *len=a;
	int *len_new;
	a[0]=1;
	a[1]=2;
	a[2]=3;
	a[3]=4;
	a[4]=5;
	len_new=(int *)realloc(len,sizeof(int)*len);

	for(i=0;i<len;i++)
	{
		printf("%d", *(len_new+i));
		getchar();
		return 0;
	} 
}

Summary

  • Memory Management in C is done in static and dynamic in which dynamic is preferred.
  • Static works at compile-time and dynamic allocation works at run-time.
  • Memory de-allocation can be done by using free() function.
  • Memory reallocation can be done by using realloc() function.

Freeing up of memory is preferable to save the memory space.