Dynamic memory allocation in C
The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime. Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file.1. malloc()
2. calloc()
3. realloc()
4. free()
Before learning above functions, let's understand the difference between static memory allocation and dynamic memory allocation.
2. calloc()
3. realloc()
4. free()
Before learning above functions, let's understand the difference between static memory allocation and dynamic memory allocation.
static memory allocation | dynamic memory allocation |
---|---|
memory is allocated at compile time. | memory is allocated at run time. |
memory can't be increased while executing program. | memory can be increased while executing program. |
used in array. | used in linked list. |
Now let's have a quick look at the methods used for dynamic memory allocation.
malloc() | allocates single block of requested memory. |
calloc() | allocates multiple block of requested memory. |
realloc() | reallocates the memory occupied by malloc() or calloc() functions. |
free() | frees the dynamically allocated memory. |
1. malloc (Memory Allocation):
Allocates a specified number of bytes of memory.
Syntax: void* malloc(size_t size);
Syntax: void* malloc(size_t size);
Example:
c#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int i, n;
// Get the number of elements from the user
printf("Enter the number of elements: ");
scanf("%d", &n);
// Allocate memory for an array of integers
arr = (int *)malloc(n * sizeof(int));
// Check if memory allocation was successful
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1; // Exit the program with an error code
}
// Input elements from the user
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Display the elements
printf("Elements entered: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
// Free the allocated memory
free(arr);
return 0;
}
yamlEnter the number of elements: 5
Enter 5 elements:
10 20 30 40 50
Elements entered: 10 20 30 40 50
2. calloc (Contiguous Allocation):
Allocates a specified number of blocks of memory, each with a specified number of bytes.
Syntax: void* calloc(size_t num, size_t size);
Syntax: void* calloc(size_t num, size_t size);
Example:
cint *arr;
arr = (int *)calloc(5, sizeof(int));
3. realloc (Re-allocation):
Changes the size of the memory block previously allocated using malloc or calloc.
Syntax: void* realloc(void* ptr, size_t size);
Syntax: void* realloc(void* ptr, size_t size);
Example:
cint *arr;
arr = (int *)malloc(5 * sizeof(int));
arr = (int *)realloc(arr, 10 * sizeof(int));
4. free:
Deallocates the memory block allocated by malloc, calloc, or realloc.
Syntax: void free(void* ptr);
Example:
Syntax: void free(void* ptr);
Example:
cint *arr;
arr = (int *)malloc(5 * sizeof(int));
free(arr);
Remember to check if the allocation was successful by verifying if the returned pointer is not NULL. Also, after using the allocated memory, always free it to avoid memory leaks.
0 Comments