Dynamic Memory Allocation in C

C is a language with some fixed rules of programming. For example: changing the size of an array is not allowed.

Dynamic Memory Allocation:

Dynamic memory allocation is a way to allocate memory to a data structure during the runtime we can use DMA function available in C to allocate and free memory during runtime.

Function for DMA in C

Following functions are available in C to perform dynamic memory allocation:

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

Dynamic memory allocation functions in c

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

malloc() function

Malloc stands for memory allocation. It takes number of bytes to be allocated as an input and returns a pointer of type void.

Syntax:

Dynamic Memory Allocation in C

The expression returns a NULL pointer if the memory cannot be allocated.

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,"Programiz.net");
    }

    printf("Dynamically allocated memory content : %s\n", mem_alloc );
    free(mem_alloc);
}

Program Output:

Dynamically allocated memory content : Programiz.net
  • 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.

Exercise: Write a program to create a dynamic array of 5 floats using malloc().

calloc() function

calloc stands for continuous allocation.

It initializes each memory block with a default value of 0.

Syntax:

ptr = (float*) calloc(30*sizeof(int)) //Allocates Contiguous space in memory for 30 blocks

If the space is not sufficient, memory allocation fails and a NULL pointer is returned.

  • 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,"Programiz.net");
    }

    printf("Dynamically allocated memory content : %s\n", mem_alloc );
    free(mem_alloc);
}

Program Output:

Dynamically allocated memory content : Programiz.net

Exercise: Write a program to create an array of size n using calloc() where n is an integer entered by the user.

free() function

We can use free() function to allocate the memory.

The memory allocated using calloc/malloc is not deallocated automatically.

Syntax:

free(ptr);      => Memory of ptr is released

Exercise: Write a program to demonstrate the usage of free() with malloc().

realloc() function

Sometimes the dynamically allocated memory is insufficient or more than required.

realloc is used to allocate memory of new size using the previous pointer and size.

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

Syntax:

ptr = realloc(ptr,newSize);
ptr = realloc(ptr, 3* sizeof(int)) //ptr now points to this new block of memory, which is capable of storing 3 integers

Exercise on Dynamic Memory Allocation

  1. Write a program to dynamically create an array of size 6 capable of storing 6 integers.
  2. Use the array in Problem 1 to store 6 integers entered by the user.
  3. Solve problem 1 using calloc().
  4. Create an array dynamically capable of storing 5 integers. Now use realloc so that it can now store 10 integers.
  5. Create an array of the multiplication table of 7 up to 10 (7×10=70). Use realloc to make it store 15 numbers(from 7×1 to 7×15).
  6. Attempt problem 4 using calloc().
Was this article helpful?
YesNo

Leave a Comment

Your email address will not be published. Required fields are marked *