malloc, calloc, or realloc are the three functions used to manipulate memory. These commonly used functions are available through the stdlib library so you must include this library to use them.

C - Dynamic memory allocation functions

Function Syntax
malloc() malloc (number *sizeof(int));
calloc() calloc (number, sizeof(int));
realloc() realloc (pointer_name, number * sizeof(int));
free() free (pointer_name);

malloc function

  • malloc function is used to allocate space in memory during the execution of the program.
  • malloc function does not initialize the memory allocated during execution.  It carries garbage value.
  • malloc function returns null pointer if it couldn't able to allocate requested amount of memory.

Example program for malloc() in C

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

void main()
{
    char *mem_alloc;
    //memory allocated dynamically
    mem_alloc = malloc( 15 * sizeof(char) );

    if(mem_alloc== NULL )
    {
        printf("Couldn't able to allocate requested memory\n");
    }
    else
    {
        strcpy( mem_alloc,"w3schools.in");
    }

    printf("Dynamically allocated memory content : %s\n", mem_alloc );
    free(mem_alloc);
}
Program Output:
Dynamically allocated memory content : w3schools.in

calloc function

  • calloc () function and malloc () function is similar. But calloc () allocates memory for zero-initializes. However, malloc () does not.

Example program for calloc() in C

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

void main()
{
    char *mem_alloc;
    //memory allocated dynamically
    mem_alloc = calloc( 15, sizeof(char) );

    if( mem_alloc== NULL )
    {
        printf("Couldn't able to allocate requested memory\n");
    }
    else
    {
        strcpy( mem_alloc,"w3schools.in");
    }

    printf("Dynamically allocated memory content : %s\n", mem_alloc );
    free(mem_alloc);
}
Program Output:
Dynamically allocated memory content : w3schools.in

realloc function

  • realloc function modifies the allocated memory size by malloc and calloc functions to new size.
  • If enough space doesn't exist in the memory of the current block to extend, a new block is allocated for the full size of reallocation, then copies the existing data to the new block and then frees the old block.

free function

  • free function frees the allocated memory by malloc (), calloc (), realloc () functions.

Example program for realloc() and free()

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

void main()
{
    char *mem_alloc;
    //memory allocated dynamically
    mem_alloc = malloc( 20 * sizeof(char) );

    if( mem_alloc == NULL )
    {
        printf("Couldn't able to allocate requested memory\n");
    }
    else
    {
        strcpy( mem_alloc,"w3schools.in");
    }

    printf("Dynamically allocated memory content  : " \ "%s\n", mem_alloc );
    mem_alloc=realloc(mem_alloc,100*sizeof(char));

    if( mem_alloc == NULL )
    {
        printf("Couldn't able to allocate requested memory\n");
    }
    else
    {
        strcpy( mem_alloc,"space is extended upto 100 characters");
    }

    printf("Resized memory : %s\n", mem_alloc );
    free(mem_alloc);
}
Program Output:
Dynamically allocated memory content : w3schools.in

Resized memory: space is extended up to 100 characters