Dynamic memory allocation in C

Dynamic memory allocation in C

We can dynamically allocate and de-allocate memory to variables in C language.  Different functions for dynamic memory allocation/de-allocation are:

  • malloc()
  • free()

1. malloc() function

malloc() is a predefined function of C language that can dynamically allocate memory to variables. It allocates a block of fixed size block from memory. Header file required for malloc() function is stdlib.h.

int *ptr;

ptr is a pointer that is allocated memory of 2 because sizeof(int) is assumed to return 2 bytes.

Program of malloc().
int main()
int *ptr;ptr = malloc(sizeof(int));

2. free() function

This function is used to deallocate memory dyanamically assigned to a variable by using malloc() function.


This command will dealloacte memory assigned to pointer variable ptr. It has been demonstrated in above program.

Spread the love
Lesson tags: calloc() function in c, dynamic memory allocation in c, dynamic memory deallocation in c, free() function in c, malloc() function in c, memory leak in c, program of dynamic memory allocation in c, program of dynamic memory deallocation in c
Back to: C Programming Language
Spread the love