In C, dynamic memory allocation allows you to allocate memory during runtime, as opposed to compile-time. This is useful when the size of the data is unknown during program compilation.
What is Dynamic Memory Allocation?
Dynamic memory allocation involves manually requesting memory during program execution using functions like:
malloc()
calloc()
realloc()
free()
The allocated memory is typically managed in the heap segment.
1. malloc()
– Memory Allocation
The malloc()
(memory allocation) function allocates a single block of memory of the specified size (in bytes). It does not initialize the memory—it contains garbage values.
Syntax
void* malloc(size_t size);
- size: The number of bytes to allocate.
- Returns: A pointer to the first byte of the allocated memory or
NULL
if allocation fails.
Example
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n = 5;
// Allocate memory for 5 integers
arr = (int*) malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
// Assign values
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
}
// Print values
printf("Array elements:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
// Free allocated memory
free(arr);
return 0;
}
Output
Array elements:
1 2 3 4 5
2. calloc()
– Contiguous Allocation
The calloc()
(contiguous allocation) function allocates multiple blocks of memory, each of the same size, and initializes all bytes to zero.
Syntax
void* calloc(size_t num, size_t size);
- num: Number of blocks.
- size: Size of each block (in bytes).
- Returns: A pointer to the allocated memory or
NULL
if allocation fails.
Example
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n = 5;
// Allocate memory for 5 integers
arr = (int*) calloc(n, sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
// Assign values
for (int i = 0; i < n; i++) {
arr[i] = (i + 1) * 2;
}
// Print values
printf("Array elements:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
// Free allocated memory
free(arr);
return 0;
}
Output
Array elements:
2 4 6 8 10
Key Differences Between malloc()
and calloc()
Feature | malloc() |
calloc() |
---|---|---|
Initialization | Does not initialize memory (contains garbage). | Initializes memory to zero. |
Number of Blocks | Allocates a single block of memory. | Allocates multiple blocks of memory. |
Syntax | malloc(size) |
calloc(num, size) |
3. Freeing Allocated Memory
The memory allocated by malloc()
or calloc()
must be manually freed using the free()
function to avoid memory leaks.
Syntax
void free(void* ptr);
Example
int *ptr = (int*) malloc(10 * sizeof(int));
free(ptr); // Deallocates the memory
Best Practices
- Always check if the pointer returned by
malloc()
orcalloc()
isNULL
before using it. - Use
free()
to release memory after use. - Avoid memory leaks by ensuring every
malloc()
orcalloc()
has a correspondingfree()
.
Let me know if you’d like to see more examples or advanced memory management techniques! 😊