Table of Contents
- Introduction to Dynamic Memory Allocation
- Using malloc() for Memory Allocation
- Using calloc() for Memory Allocation
- Using realloc() for Memory Reallocation
- Freeing Dynamically Allocated Memory
- Example Programs
Introduction to Dynamic Memory Allocation
In C programming, memory can be allocated dynamically during program execution. Dynamic memory allocation allows you to request and manage memory at runtime, which is especially useful when you need to work with data structures whose size is not known at compile time.
Using malloc() for Memory Allocation
The malloc() function is used to allocate a block of memory from the heap. It takes the number of bytes as an argument and returns a pointer to the allocated memory. Here's the syntax:
ptr = (castType*) malloc(size);
For example, to allocate memory for an integer:
int *ptr;
ptr = (int*) malloc(sizeof(int));
Using calloc() for Memory Allocation
The calloc() function is used to allocate a block of memory for an array of elements, each initialized to zero. It takes the number of elements and the size of each element as arguments and returns a pointer to the allocated memory. Here's the syntax:
ptr = (castType*) calloc(numElements, size);
For example, to allocate memory for an array of 5 integers:
int *ptr;
ptr = (int*) calloc(5, sizeof(int));
Using realloc() for Memory Reallocation
The realloc() function is used to resize a previously allocated block of memory. It takes a pointer to the previously allocated memory, the new size, and returns a pointer to the reallocated memory. Here's the syntax:
ptr = (castType*) realloc(ptr, newSize);
For example, to resize the previously allocated memory for the integer array:
ptr = (int*) realloc(ptr, 10 * sizeof(int));
Freeing Dynamically Allocated Memory
It's important to release the dynamically allocated memory when it's no longer needed to prevent memory leaks. The free() function is used to deallocate memory that was previously allocated using malloc(), calloc(), or realloc().
free(ptr);
Example Programs
Program 1: Using malloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int*) malloc(sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
*ptr = 42;
printf("Value: %d\n", *ptr);
free(ptr);
return 0;
}
Program 2: Using calloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int*) calloc(5, sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
for (int i = 0; i < 5; i++) {
ptr[i] = i + 1;
}
for (int i = 0; i < 5; i++) {
printf("Value at index %d: %d\n", i, ptr[i]);
}
free(ptr);
return 0;
}
Conclusion
Dynamic memory allocation in C using malloc(), calloc(), and realloc() provides flexibility in managing memory at runtime. Remember to always release the allocated memory using free() to prevent memory leaks. By understanding and utilizing dynamic memory allocation, you can create more versatile and memory-efficient programs.
0 Comments