Loading content...
Loading content...
Dynamic Memory Allocation (DMA) is a technique that allows memory to be allocated, resized, and released during program execution.
Unlike normal variables that are stored in the stack, dynamically allocated memory is stored in the heap and can be managed according to program requirements.
In many situations, we do not know how much memory will be required before the program starts.
Dynamic Memory Allocation helps to:
Allocate memory at runtime.
Increase or decrease memory size when needed.
Efficiently use system memory.
Store data whose size is not known in advance.
Return memory from functions safely.
Dynamic memory is allocated from the Heap Segment.
Memory is allocated during runtime.
Managed by the programmer.
Remains available until explicitly released.
Shared across the entire program.
The following functions are available in the <stdlib.h> header file:
plaintext
malloc()
calloc()
realloc()
free()malloc() stands for Memory Allocation.
It allocates a single block of memory at runtime.
plaintext
ptr = (datatype *)malloc(size_in_bytes);plaintext
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
ptr = (int *)malloc(sizeof(int) * 5);
for(int i = 0; i < 5; i++)
{
ptr[i] = i + 1;
}
for(int i = 0; i < 5; i++)
{
printf("%d ", ptr[i]);
}
free(ptr);
return 0;
}plaintext
1 2 3 4 5Allocates memory from the heap.
Memory contains garbage values initially.
Returns a void pointer.
Returns NULL if allocation fails.
Always verify whether memory allocation was successful.
plaintext
int *ptr = (int *)malloc(sizeof(int) * 5);
if(ptr == NULL)
{
printf("Memory Allocation Failed");
return 0;
}calloc() stands for Contiguous Allocation.
It allocates memory for multiple elements and initializes all bytes to zero.
plaintext
ptr = (datatype *)calloc(number_of_elements,
size_of_each_element);plaintext
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
ptr = (int *)calloc(5, sizeof(int));
for(int i = 0; i < 5; i++)
{
printf("%d ", ptr[i]);
}
free(ptr);
return 0;
}plaintext
0 0 0 0 0Allocates memory.
Memory contains garbage values.
Takes one argument.
Example:
plaintext
malloc(5 * sizeof(int));Allocates memory.
Initializes memory with zero.
Takes two arguments.
Example:
plaintext
calloc(5, sizeof(int));The free() function releases dynamically allocated memory back to the operating system.
plaintext
free(pointer);plaintext
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
ptr = (int *)malloc(sizeof(int));
*ptr = 100;
printf("%d\n", *ptr);
free(ptr);
return 0;
}plaintext
100After freeing memory, assign NULL to the pointer.
plaintext
free(ptr);
ptr = NULL;This prevents the pointer from becoming a dangling pointer.
A pointer that points to memory that has already been freed is called a dangling pointer.
plaintext
int *ptr;
ptr = (int *)malloc(sizeof(int));
free(ptr);
/* ptr is now dangling */plaintext
free(ptr);
ptr = NULL;realloc() is used to resize an existing memory block.
It can:
Increase memory size.
Decrease memory size.
plaintext
ptr = (datatype *)realloc(ptr, new_size);plaintext
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
ptr = (int *)malloc(5 * sizeof(int));
ptr = (int *)realloc(ptr,
10 * sizeof(int));
free(ptr);
return 0;
}The memory block is expanded from 5 integers to 10 integers.
Always use a temporary pointer.
plaintext
int *temp;
temp = (int *)realloc(ptr,
10 * sizeof(int));
if(temp != NULL)
{
ptr = temp;
}If realloc() fails:
plaintext
ptr = realloc(ptr, size);may lose the original memory block and create a memory leak.
Using a temporary pointer prevents this issue.
plaintext
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
ptr = (int *)malloc(5 * sizeof(int));
if(ptr == NULL)
{
printf("Allocation Failed");
return 0;
}
ptr = (int *)realloc(ptr,
8 * sizeof(int));
if(ptr == NULL)
{
printf("Reallocation Failed");
return 0;
}
for(int i = 0; i < 5; i++)
{
ptr[i] = (i + 1) * 10;
}
for(int i = 0; i < 5; i++)
{
printf("%d ", ptr[i]);
}
free(ptr);
return 0;
}plaintext
10 20 30 40 50plaintext
malloc()
↓
Use Memory
↓
realloc() (Optional)
↓
Use Memory
↓
free()Occurs when allocated memory is not released.
plaintext
int *ptr;
ptr = (int *)malloc(sizeof(int));
/* free() not called */plaintext
free(ptr);Occurs when memory is freed but pointer still stores the old address.
plaintext
free(ptr);
printf("%d", *ptr);plaintext
free(ptr);
ptr = NULL;If memory is unavailable, allocation functions return NULL.
plaintext
if(ptr == NULL)
{
printf("Allocation Failed");
}Repeated allocation and deallocation may create small unused memory blocks in the heap.
This can reduce memory efficiency over time.
Efficient memory utilization.
Memory allocated when needed.
Flexible data structures can be created.
Supports dynamic arrays.
Required for linked lists, stacks, queues, trees, and graphs.
Slower than stack allocation.
Programmer must manage memory manually.
Memory leaks may occur.
Dangling pointers may occur.
Memory fragmentation can happen.
Dynamic Memory Allocation is widely used in:
Dynamic Arrays
Linked Lists
Stacks
Queues
Trees
Graphs
Database Systems
Operating Systems
Dynamic Memory Allocation allows memory management during runtime.
Memory is allocated from the Heap Segment.
malloc() allocates memory with garbage values.
calloc() allocates memory initialized to zero.
realloc() changes the size of an existing memory block.
free() releases memory back to the system.
Always check for NULL and free allocated memory to avoid memory leaks.