Dynamic Memory Allocation
In C language, dynamic memory allocation allows you to allocate memory at runtime, rather than at compile time. This is useful when you don’t know the size of the memory that you need until the program is running.
C provides several functions for dynamic memory allocation, including malloc
, calloc
, realloc
, and free
. These functions are defined in the stdlib.h
header file.
Here is a brief overview of each function:
malloc
:
This function is used to allocate a block of memory of a specified size. The syntax for using malloc
is as follows:
void *malloc(size_t size);
Here, size
is the number of bytes to allocate, and the function returns a void pointer to the allocated memory block. You can then cast this pointer to the appropriate data type.
calloc
:
This function is used to allocate a block of memory and initialize it to all zeros. The syntax for using calloc
is as follows:
void *calloc(size_t nmemb, size_t size);
Here, nmemb
is the number of elements to allocate, and size
is the size of each element in bytes. The function returns a void pointer to the allocated and initialized memory block.
realloc
:
This function is used to change the size of a previously allocated memory block. The syntax for using realloc
is as follows:
void *realloc(void *ptr, size_t size);
Here, ptr
is a pointer to the memory block to be reallocated, and size
is the new size of the memory block. The function returns a void pointer to the newly allocated memory block. Note that the original memory block may be moved to a different location in memory.
free
:
This function is used to release a previously allocated memory block. The syntax for using free
is as follows:
void free(void *ptr);
Here, ptr
is a pointer to the memory block to be released. After calling free
, the memory block is no longer available for use.
Here is an example of how to use malloc
to allocate memory for an array of integers:
#include <stdlib.h>
#include <stdio.h>
int main() {
int *my_array;
int array_size = 10;
my_array = (int *) malloc(array_size * sizeof(int));
if (my_array == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
for (int i = 0; i < array_size; i++) {
my_array[i] = i + 1;
printf("%d\n", my_array[i]);
}
free(my_array);
return 0;
}
In this example, malloc
is used to allocate memory for an array of integers with a size of array_size
, which is set to 10. The sizeof
operator is used to get the size of an integer in bytes, so that the total number of bytes to allocate is array_size * sizeof(int)
. The function returns a void pointer to the allocated memory block, which is then cast to a pointer to an integer ((int *)
). The program then checks if the memory allocation was successful by checking if the returned pointer is NULL
. If it is, the program exits with an error message. If the allocation was successful, the program initializes the elements of the array and prints them to the console. Finally, the program calls free
to release the memory allocated for the array.