Skip to main content

26. Dynamic Memory Allocation

 

Static and Dynamic Memory Allocation

Static Memory AllocationDynamic Memory Allocation
Memory is allocated at compile time (before the program's run).Memory is allocated at run time (during the program's execution ).
There is no memory reusability and the memory allocated cannot be freed.There is memory reusability and the allocated memory can be freed when not required.
Memory can't be increased while executing program.Memory can be increased while executing program.
Less efficient.More efficient.

Dynamic Memory Allocation in C

It enables the C programmer to allocate memory at runtime. Dynamic memory allocation in C language is possible by 4 functions of stdlib.h header file.

  • malloc()
  • calloc()
  • realloc()
  • free()
malloc()allocates single block of requested memory.
calloc()allocates multiple block of requested memory.
realloc()reallocates the memory occupied by malloc() or calloc() functions.
free()frees the dynamically allocated memory.

malloc() function in C

malloc() stands for memory allocation. It reserves a block of memory with the given amount of bytes. The return value is a void pointer to the allocated size. Therefore, the void pointer needs to be casted to the appropriate type as per the requirement. It doesn't initialize memory at execution time, so it has garbage value initially. It returns NULL if memory is not sufficient. The syntax of malloc() function is given below:

ptr = (ptr_type *)malloc(size_in_bytes);

#include <stdio.h>
#include <stdlib.h>
int main()
{
   int n, i, *ptr, sum = 0;
   printf("Enter number of elements: ");
   scanf("%d", &n);
   ptr = (int *)malloc(n * sizeof(int));
   if(ptr == NULL){
      printf("Sorry! unable to allocate memory");
      exit(0);
   }
   for(i = 0; i < n; i++){
      printf("Enter element %d: ", i+1);
      scanf("%d", ptr+i); 
// ptr[i] = ptr + i
      sum += *(ptr+i);
 // ptr[i] = ptr + i
   }
   printf("Sum = %d", sum);
   return 0;
}

Output :

Enter number of elements: 5
Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5
Sum = 15

calloc() function in C

calloc() stands for contiguous allocation. It reserves n blocks of memory with the given amount of bytes. The return value is a void pointer to the allocated space. Therefore, the void pointer needs to be casted to the appropriate type as per the requirements. It initially initializes all bytes to zero. It returns NULL if memory is not sufficient. The syntax of calloc() function is given below:

ptr = (ptr_type *)calloc(number, size_in_bytes);

#include <stdio.h>
#include <stdlib.h>
int main()
{
   int n, i, *ptr, sum = 0;
   printf("Enter number of elements: ");
   scanf("%d", &n);
   ptr = (int *)calloc(n, sizeof(int));
   if(ptr == NULL){
      printf("Sorry! unable to allocate memory");
      exit(0);
   }
   for(i = 0; i < n; i++){
      printf("Enter element %d: ", i+1);
      scanf("%d", ptr+i);
 // ptr[i] = ptr + i
      sum += *(
ptr+i); // ptr[i] = ptr + i

   }
   printf("Sum = %d", sum);
   return 0;
}

Output :

Enter number of elements: 5
Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5
Sum = 15

realloc() function in C

realloc() stands for reallocation. If memory is not sufficient for malloc() or calloc(), we can reallocate the memory by realloc() function. In short, it changes the memory size. The syntax of realloc() function is given as:

ptr = (ptr_type*) realloc(ptr, new_size_in_bytes);

free() function in C

free() is used to free the allocated memory. The memory occupied by malloc() or calloc() or realloc() functions must be released by calling free() function. Otherwise, it will consume memory until program exit. The syntax of free() function is given below:

free(ptr);

Memory Leak in C

Memory leak is caused when we don't use dynamic memory properly. When we keep on allocating dynamic memory without freeing, the overall memory usage keeps on increasing. This situation is the cause of memory leak i.e. programmer creates a memory block in the memory and forgets to delete it.To avoid these memory leak situations, memory allocated dynamically should be always be freed when not needed.

Comments