Need more memory? Dynamic memory to the rescue!
In C, a program can obtain memory at runtime through a process called dynamic memory allocation. This is crucial when you need to allocate memory for data whose size changes while the application is running or when the quantity of memory required is unknown at compile time.
Why Dynamic Memory Allocation?
Flexibility: It allows for the allocation of memory as needed at runtime, which is particularly useful for data structures whose size cannot be determined beforehand, like linked lists or dynamic arrays.
Efficiency: It helps in managing memory usage more efficiently by allocating only the required amount of memory, thus minimizing waste.
Control: It gives the programmer direct control over the memory, allowing for the creation of complex data structures and the management of their lifetimes.
How Dynamic Memory Allocation Works:
In C, dynamic memory allocation is managed through a set of functions in the C standard library (stdlib.h
):
malloc
: Allocates a block of memory of the specified size and returns a pointer to the beginning of the block.calloc
: Allocates space for an array of elements, initializes them to zero, and then returns a pointer to the memory.realloc
: Resizes a previously allocated memory block, keeping as much of the old data as will fit in the new size.free
: Deallocates a previously allocated block of memory, making it available again for future allocations.
Sample Code Using Dynamic Memory Allocation Functions:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n = 5; // Suppose we want an array for 5 integers
// Allocate memory using malloc
arr = (int*)malloc(n * sizeof(int));
if (arr == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return 1;
}
// Initialize the array
for (int i = 0; i < n; i++) {
arr[i] = i;
}
// Resize the array to hold 10 integers using realloc
int new_size = 10;
arr = (int*)realloc(arr, new_size * sizeof(int));
if (arr == NULL) {
fprintf(stderr, "Memory reallocation failed\n");
return 1;
}
// Free the allocated memory
free(arr);
return 0;
}
Explanation of Functions:
malloc(size_t size)
: Allocatessize
bytes of uninitialized storage. If allocation succeeds, a pointer to the allocated memory is returned. If it fails,NULL
is returned.calloc(size_t num, size_t size)
: Allocates space for an array ofnum
elements, each of whose size in bytes issize
. The space is initialized to zero.realloc(void *ptr, size_t new_size)
: Attempts to resize the memory block pointed to byptr
that was previously allocated with a call tomalloc
orcalloc
. Ifrealloc
is successful, it returns a pointer to the newly allocated memory, which may be the same asptr
or a new location. Ifptr
isNULL
,realloc
behaves likemalloc
for the specified size.free(void *ptr)
: Deallocates the memory block pointed to byptr
, which must have been returned by a previous call tomalloc
,calloc
, orrealloc
. Ifptr
isNULL
, no action occurs.
To confirm that the allocation was successful, it is crucial to constantly examine the return value of these functions. To prevent memory leaks, any block of memory that has been allocated using malloc, calloc, or realloc should also be freed with free when it is no longer required.
Subscribe to my newsletter
Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Jyotiprakash Mishra
Jyotiprakash Mishra
I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.