Dynamic Memory Allocation (DMA) allows a program to allocate and deallocate memory at runtime using functions provided in stdlib.h.
Memory size can be decided at runtime
Efficient use of memory
Useful when size of data is unknown in advance
| Function | Purpose |
|---|---|
malloc() | Allocates single block of memory |
calloc() | Allocates multiple blocks and initializes to zero |
realloc() | Resizes previously allocated memory |
free() | Releases allocated memory |
Allocates memory without initialization.
Allocates memory and initializes all bytes to zero.
Changes the size of previously allocated memory.
Releases allocated memory.
| Feature | malloc | calloc |
|---|---|---|
| Initialization | No | Yes (zero) |
| Arguments | One | Two |
| Speed | Faster | Slower |
Efficient memory usage
Flexible memory allocation
Prevents memory wastage
Always check for NULL pointer
Free memory after use
Avoid memory leaks
DMA allocates memory at runtime
stdlib.h provides DMA functions
malloc, calloc, realloc, and free are used
Proper memory management is essential
Take quizzes related to this topic and see where you stand!
Start Quiz Now