Dynamic memory allocation is a process in computer programming where memory is
allocated at runtime, rather than compile time. This allows programs to request memory as
needed during execution. One common area where dynamic memory allocation is used is
with the heap.
The heap is a region of memory where dynamic memory allocation occurs. It's a large pool of
memory that the program can use to dynamically allocate memory blocks of varying sizes.
Unlike the stack, which is used for static memory allocation and follows a last-in, first-out
(LIFO) approach, the heap memory allocation follows a more flexible allocation and
deallocation pattern.
In languages like C and C++, memory on the heap is managed explicitly by the programmer
using functions like malloc(), calloc(), realloc(), and free().
#include <stdio.h>
#include <stdlib.h>
int main() {
// Using malloc to allocate memory for 5 integers
int *ptr = (int *)malloc(5 * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Assigning values to the allocated memory
for (int i = 0; i < 5; i++) {
ptr[i] = i + 1;
}
// Printing the values
printf("Values stored in memory allocated by malloc:\n");
for (int i = 0; i < 5; i++) {
printf("%d ", ptr[i]);
}
printf("\n");
// Using calloc to allocate memory for 5 integers
int *ptr2 = (int *)calloc(5, sizeof(int));
if (ptr2 == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// calloc initializes the allocated memory to zero
// Printing the values
printf("Values stored in memory allocated by calloc:\n");
for (int i = 0; i < 5; i++) {
printf("%d ", ptr2[i]);
}
printf("\n");
// Using realloc to resize the previously allocated memory to hold 10 integers
ptr = (int *)realloc(ptr, 10 * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Assigning values to the newly resized memory
for (int i = 5; i < 10; i++) {
ptr[i] = i + 1;
}
// Printing the values
printf("Values stored in memory after realloc:\n");
for (int i = 0; i < 10; i++) {
printf("%d ", ptr[i]);
}
printf("\n");
// Freeing the dynamically allocated memory
free(ptr);
free(ptr2);
return 0;
}
Explanation:
malloc(size_t size): Allocates memory block of specified size in bytes. It returns a pointer to
the allocated memory or NULL if allocation fails.
calloc(size_t num, size_t size): Allocates memory block for an array of num elements, each
of them size bytes long, and initializes all its bits to zero. It returns a pointer to the allocated
memory or NULL if allocation fails.
realloc(void *ptr, size_t size): Resizes the memory block pointed to by ptr to be size bytes
long. It returns a pointer to the newly allocated memory, which may be different from ptr, or
NULL if allocation fails. If ptr is NULL, the behavior is the same as calling malloc(size). If
size is 0, the behavior is implementation-defined: the new memory block may be allocated or
the pointer may be freed.
free(void *ptr): Deallocates the memory previously allocated by malloc, calloc, or realloc.
In the example:
Memory is allocated for 5 integers using malloc.
Values are assigned to the allocated memory.
Memory is allocated for another 5 integers using calloc, and the memory is initialized to zero.
Memory is resized to hold 10 integers using realloc.
Values are assigned to the newly resized memory.
Finally, the dynamically allocated memory is freed using free.