The perfect place for easy learning...

C Programming Language

×

Topics List

Place your ad here

Dynamic Memory Allocation in C

In C programming language, when we declare variables memory is allocated in space called stack. The memory allocated in stack is fixed at the time of compilation and remains till end the program execution. When we create an array, we must specify the size at the time of declaration itself and it can not be changed during the program execution. This is a major problem when we does not know the number of values to be stored in an array. To solve this we use the concept of Dynamic Memory Allocation. The dynamic memory allocation allocates memory from heap storage. Dynamic memory allocation is defined as follow...

Allocation of memory during the program execution is called dynamic memory allocation.

or

Dynamic memory allocation is the process of allocating the memory manually at the time of program execution.

We use pre-defined or standard library functions to allocate memory dynamically. There are FOUR standard library functions that are defined in the header file known as "stdlib.h". They are as follows...

  1. malloc()
  2. calloc()
  3. realloc()
  4. free()

malloc()

malloc() is the standard library function used to allocate a memory block of specified number of bytes and returns void pointer. The void pointer can be casted to any datatype. If malloc() function unable to allocate memory due to any reason it returns NULL pointer.

Syntax

void* malloc(size_in_bytes)

Example

Example Program for malloc().

#include<stdio.h>
#include<conio.h>

int main () {

   char *title;

   title = (char *) malloc(15);
   
   strcpy(title, "c programming");
   printf("String = %s,  Address = %u\n", title, title);
   
   return(0);
   
}

calloc()

calloc() is the standard library function used to allocate multiple memory blocks of specified number of bytes and initializes them to ZERO. calloc() function returns void pointer. If calloc() function unable to allocate memory due to any reason it returns NULL pointer. Generelly calloc() is used to allocate memory for array and structure. calloc() function takes two arguments and they are 1. Number of blockas to be allocate, 2. Size of each block in bytes

Syntax

void* calloc(number_of_blocks, size_of_each_block_in_bytes)

Example

Example Program for calloc().

#include<stdio.h>
#include<conio.h>

int main () {
   int i, n;
   int *ptr;

   printf("Number of blocks to be created:");
   scanf("%d",&n);

   ptr = (int*)calloc(n, sizeof(int));
   printf("Enter %d numbers:\n",n);
   for( i=0 ; i < n ; i++ ) {
      scanf("%d",&ptr[i]);
   }

   printf("The numbers entered are: ");
   for( i=0 ; i < n ; i++ ) {
      printf("%d ",ptr[i]);
   }
   
   return(0);
}

realloc()

realloc() is the standard library function used to modify the size of memory blocks that were previously allocated using malloc() or calloc(). realloc() function returns void pointer. If calloc() function unable to allocate memory due to any reason it returns NULL pointer.

Syntax

void* realloc(*pointer, new_size_of_each_block_in_bytes)

Example

Example Program for realloc().

#include<stdio.h>
#include<conio.h>

int main () {

   char *title;

   title = (char *) malloc(15);
   
   strcpy(title, "c programming");
   printf("Before modification : String = %s,  Address = %u\n", title, title);
   
   title = (char*) realloc(title, 30);
   
   strcpy(title,"C Programming Language");
   printf("After modification : String = %s,  Address = %u\n", title, title);
   
   return(0);
   
}

free()

free() is the standard library function used to deallocate memory block that was previously allocated using malloc() or calloc(). free() function returns void pointer. When free() function is used with memory allocated that was created using calloc(), all the blocks are get deallocated.

Syntax

void free(*pointer)

Example

Example Program for free().

#include<stdio.h>
#include<conio.h>

int main () {

   char *title;

   title = (char *) malloc(15);
   
   strcpy(title, "c programming");
   printf("Before modification : String = %s,  Address = %u\n", title, title);
   
   title = (char*) realloc(title, 30);
   
   strcpy(title,"C Programming Language");
   printf("After modification : String = %s,  Address = %u\n", title, title);
   
   free(title);
   
   return(0);
   
}

Place your ad here
Place your ad here