C Programming - SPLessons

Memory Management in C

Home > Lesson > Chapter 34
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Memory Management in C

Memory Management in C

shape Description

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.

shape Conceptual figure

Memory is allocated to various data in two ways :

Static Memory Allocation

shape Description

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

shape Description

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.

shape malloc()

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.

shape Syntax

ptr =(cast_type*) malloc(size) Here, ptr is pointer of cast type.

shape Example

[c] #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; } [/c] After using malloc() function memory must be restored using de-allocation.

De-allocation of memory:

shape free()

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.

shape Syntax

ptr= void free(void *ptr);

shape Example

[c] #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; } [/c] use free(p); in the above program before return 0;

shape calloc()

calloc() will function in an advanced way by tracking the address of the memory. This can be used for dynamic allocation of arrays.

shape Syntax

ptr =(cast_type*) calloc(count,size);

shape Example

[c] #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; }[/c]

Reallocation of memory

shape realloc()

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.

shape Syntax

ptr= (void*)realloc(void  *ptr, size);

shape Examples

[c] #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; } }[/c]

Summary

shape Key Points

  • 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.

shape Programming Tips

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