Dynamic Memory Allocation in C: Introduction to malloc(), calloc(), free(), and realloc()

In C programming, dynamic memory allocation (DMA) is a critical concept that gives your programs the flexibility to allocate memory during runtime. Unlike static memory, which is fixed at compile time, DMA allows you to create variables, arrays, or structures on-the-fly using standard library functions such as malloc(), calloc(), realloc(), and free().


What is Dynamic Memory Allocation?

Dynamic memory allocation allows a program to:

  • Allocate memory at runtime.
  • Use only as much memory as required.
  • Efficiently manage large or variable-size data structures.

Memory is allocated from the heap area in RAM and can be released when no longer needed.


Key Functions for DMA in C

1. malloc() – Memory Allocation

Allocates a block of memory of specified size and returns a pointer to it. Memory is uninitialized.

int *arr;
arr = (int *)malloc(5 * sizeof(int));

Allocates memory for 5 integers.


2. calloc() – Contiguous Allocation

Allocates memory for an array of elements, initializing all to zero.

int *arr;
arr = (int *)calloc(5, sizeof(int));

Allocates and initializes 5 integers to 0.


3. realloc() – Reallocate Memory

Changes the size of previously allocated memory block.

arr = (int *)realloc(arr, 10 * sizeof(int));

Expands memory block to hold 10 integers.


4. free() – Free Memory

Releases dynamically allocated memory to avoid memory leaks.

free(arr);

Example: Using malloc() and free()

#include <stdio.h>
#include <stdlib.h>

int main() {
int *ptr, n, i;

printf("Enter number of elements: ");
scanf("%d", &n);

ptr = (int *)malloc(n * sizeof(int));

if (ptr == NULL) {
printf("Memory allocation failed!");
return 1;
}

printf("Enter elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &ptr[i]);
}

printf("You entered:\n");
for (i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}

free(ptr);
return 0;
}

malloc vs calloc – Key Differences

Featuremalloc()calloc()
InitializationNo (garbage data)Yes (zero initialized)
ParametersOneTwo
SpeedSlightly fasterSlightly slower

When to Use realloc()

Use realloc() when you need to:

  • Increase or decrease the size of an array.
  • Adjust memory allocation based on user input or program state.

Always Free Allocated Memory

Memory leaks happen when allocated memory is not released. Always use free() once you’re done using dynamic memory to keep your program efficient and clean.

Key Takeaways

  • Dynamic memory allocation in C provides flexibility and efficient memory use.
  • Use malloc() or calloc() to allocate memory at runtime.
  • Use realloc() to resize memory dynamically.
  • Always use free() to release memory.

Previous Article

Introduction to Dynamic Memory Allocation in C: malloc, calloc, and free

Next Article

Implement Stack in C Using Array: Push, Pop, Display Operations

Write a Comment

Leave a Comment

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

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨