Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

C

Learn step by step



C - Dynamic memory allocation

Process of allocating the memory during program execution or run time allocation of memory is called dynamic memory allocation.

Functions of dynamic memory allocation in C:

C language offers following 4 dynamic memory allocation functions.

  • malloc()
  • calloc()
  • realloc()
  • free()
S.no
Function
Syntax
1 malloc () malloc (number *sizeof(int));
2 calloc () calloc (number, sizeof(int));
3 realloc () realloc (pointer_name, number * sizeof(int));
4 free () free (pointer_name);
1. malloc() function in C:
  • malloc () function is used to allocate space in memory during the execution of the program.
  • malloc () does not initialize the memory allocated during execution. It carries garbage value by default.
  • malloc () function returns null pointer if it couldn't able to allocate requested amount of memory.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
char *mem_allocation;
/* memory is allocated dynamically */
mem_allocation = malloc( 20 * sizeof(char) );
if( mem_allocation== NULL )
{
printf("Couldn't able to allocate requested memory\n");
}
else
{
strcpy( mem_allocation,"Orianaitsolution.com");
}
printf("Dynamically allocated memory content : " \
"%s\n", mem_allocation );
free(mem_allocation);
}

Output

Dynamically allocated memory content : Orianaitsolution.com
3. realloc() function in C:
  • realloc () function modifies the allocated memory size by malloc () and calloc () functions to new size.
  • If enough space does not exist in memory of current block to extend, new block is allocated for the full size of reallocation, then copies the existing data to new block and then frees the old block.
4. free() function in C:
  • free () function frees the allocated memory by malloc (), calloc (), realloc () functions and returns the memory to the system.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
char *mem_allocation;
/* memory is allocated dynamically */
mem_allocation = malloc( 20 * sizeof(char) );
if( mem_allocation == NULL )
{
printf("Couldn't able to allocate requested memory\n");
}
else
{
strcpy( mem_allocation,"TutOn-Redefining Online Education");
}
printf("Dynamically allocated memory content : " \
"%s\n", mem_allocation );
mem_allocation=realloc(mem_allocation,100*sizeof(char));
if( mem_allocation == NULL )
{
printf("Couldn't able to allocate requested memory\n");
}
else
{
strcpy( mem_allocation,"space is extended upto " \
"100 characters");
}
printf("Resized memory : %s\n", mem_allocation );
free(mem_allocation);
}
Output:

Code

Dynamically allocated memory content : TutOn-Redefining Online Education Resized memory : space is extended upto 100 characters
Major difference between static allocation and dynamic allocation of memory in C:
S.no Static memory allocation Dynamic memory allocation
1 In static memory allocation, memory is allocated while writing the C program. User requested memory will be allocated at compile time. In dynamic memory allocation, memory is allocated while executing the program. Means at run time.
2 Memory size can't be modified while execution. 
Example: array
Memory size can be modified while execution. 
Example: Linked list
Difference between malloc() and calloc() functions in C:
S.no malloc() calloc()
1 It allocates only single block of requested memory It allocates multiple blocks of requested memory
2 int *ptr;ptr = malloc( 20 * sizeof(int) );In this 20*4 bytes of memory only allocated in one block.
Total = 80 bytes
int *ptr;Ptr = calloc( 20, 20 * sizeof(int) );In this 20 blocks of memory will be created and each contains 20*4 bytes of memory. 
Total = 1600 bytes
3 malloc () doesn't initializes the allocated memory. It contains garbage values calloc () initializes the allocated memory to zero
4 type cast must be done since this function returns void pointer int *ptr;ptr = (int*)malloc(sizeof(int)*20 ); Same as malloc () function float *ptr;ptr = (float*)calloc( 20, 20 * sizeof(float) );


Related Videos