The functions used to manipulate memory in C programming are malloc(), calloc(), and realloc(). These commonly used functions are available through the stdlib() library, so you must include this library in your program 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 allocates space in memory during the program's execution.
  • malloc function does not initialize the space in memory allocated during execution. It carries garbage value.
  • malloc function returns a null pointer if it couldn't allocate the 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

  • The calloc() 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

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

free function

The free() function releases the memory allocated by the 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



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram