Dynamic Memory Allocation

Dynamic Memory Allocation

Dynamic Memory Allocation:-

Every program allocates memory during the runtime and load time, but how much memory will be allocated during the runtime is called dynamic memory allocation.

If the size is decided during the compile time only that called static memory allocation.

Entire 4GB memory is split into two parts such as lower 3GB memory is called as user space and upper 1GB memory is called kernel space.

Any variable any function in programming language allocates memory in user space area but the device driver program only allocates memory in kernel space area.

Userspace memory is again divided into 5 segments that are:-

  1. Stack
  2. Heap
  3. BSS
  4. Data
  5. Text

Dynamic Memory Allocation in C is a part of Heap area.

Memory allocation in C is possible through variables and functions.

Basically dynamic memory allocation is done by a family of 3 functions in C, such as malloc( ), calloc( ) and realloc().

malloc and calloc:-

Malloc, calloc, and realloc always allocate memory above the break pointer.

Malloc and calloc function allocate memory in the heap segment.

Malloc returns null pointer when unable to allocate memory in heap segment.

Memory allocation using malloc and calloc is always the same.

Syntax of malloc:-

ptr = (cast-type*) malloc(byte-size)

Malloc and calloc also return the null pointer when it unable to allocate a memory.

Malloc and calloc function both return generic pointer and also realloc return generic pointer.

Syntax of Calloc:-

ptr = (cast-type*)calloc(n, element-size);

Difference in malloc and calloc is:-

Memory allocation in malloc is single dimensional array equivalent and memory allocation in calloc is double dimensional array equivalent.


Resize the memory block by deallocating again allocating.

All other functions allocate memory in the stack segment.

Syntax of Realloc:-

ptr = realloc(ptr, newsize);

The non-static local variable also created in stack segment.

Static initialize variable is created in the data segment but static uninitialized variable is created in BSS segment.

Realloc allocates memory always relative to the last block of malloc or calloc to resize the memory block.


Free() is used to deallocate the memory after free the pointers become dangling.

Free() is used to deallocate for memory. Free()  can not deallocate array memory.

Syntax of Free:-



  • Break pointer is initial entry point in heap.
  • brk and sbrk function is used to set and retrieve the break pointer
  • Memory allocated in heap remains in existence for the duration of a program.
  • When memory is allocated from heap area using functions, their addresses are growing upwards and heap size is decreasing and
  • when memory is free heap size is increasing.
  • Free means break pointer is decreasing in last break pointer – malloc size – 8
  • Static allocation: When you declare a static and global variable in C program that reserves block of space, of a fixed size when program.
  • Static allocation is a part of Data or BSS area.
  • Automatic allocation: When you declare automatic variables, such as function actual argument and local variables in C program that reserves
  • memory when control entered in scope and automatically free when a control leaves the scope.
  • Automatic memory allocation is a part of stack area.
  • Dynamic memory allocation is not supported by C variables: there is no storage class called “dynamic” to allocate memory dynamically.
  • After free Pointer should be initialized with 0 to overcome the core dump.


A Computer Science Study for IT students and people of IT community

Leave a Reply

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