Dynamic Memory in C
Dynamic memory allocation in C allows you to allocate memory at runtime. This is useful when the size of data structures or variables is not known before the program runs. The malloc(), calloc(), realloc(), and free() functions in C are used for dynamic memory management.
Definition
Dynamic memory allocation enables programs to use memory efficiently by allocating memory as needed and freeing it when no longer required. This type of memory allocation is done using pointers and allows for flexible memory management at runtime.
Functions for Dynamic Memory Allocation
- malloc(): Allocates a specified number of bytes of memory and returns a pointer to the first byte of allocated memory.
- calloc(): Similar to malloc() but initializes the memory to zero.
- realloc(): Resizes previously allocated memory to a new size.
- free(): Frees previously allocated memory.
Syntax
Here are the basic syntaxes for these functions:
Syntax:
pointer = (data_type*) malloc(size_in_bytes); // malloc
pointer = (data_type*) calloc(num_elements, size_of_element); // calloc
pointer = (data_type*) realloc(pointer, new_size_in_bytes); // realloc
free(pointer); // free
Example: Using malloc and free
This example demonstrates how to dynamically allocate and deallocate memory using malloc() and free().
Example Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *arr, i, n;
// Ask the user to enter the number of elements
printf("Enter number of elements: ");
if (scanf("%d", &n) != 1) {
printf("Invalid input.\n");
return 1;
}
// Dynamically allocate memory for n elements
arr = (int*) malloc(n * sizeof(int));
// Check if malloc was successful
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
// Input the elements
printf("Enter the elements:\n");
for (i = 0; i < n; i++) {
if (scanf("%d", &arr[i]) != 1) {
printf("Invalid input.\n");
free(arr); // Free the allocated memory before exiting
return 1;
}
}
// Output the elements
printf("Entered elements are:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Free the dynamically allocated memory
free(arr);
return 0;
}
Output
Example: Using calloc and realloc
This example demonstrates how to use calloc() and realloc() to allocate memory for an array of integers.
Example :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *arr, i, n;
// Ask the user to enter the number of elements
printf("Enter number of elements: ");
if (scanf("%d", &n) != 1) {
printf("Invalid input.\n");
return 1;
}
// Dynamically allocate memory for n elements
arr = (int*) malloc(n * sizeof(int));
// Check if malloc was successful
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
// Input the elements
printf("Enter the elements: \n");
for (i = 0; i < n; i++) {
if (scanf("%d", &arr[i]) != 1) {
printf("Invalid input.\n");
free(arr); // Free the allocated memory before exiting
return 1;
}
}
// Output the elements
printf("Entered elements are: \n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Free the dynamically allocated memory
free(arr);
return 0;
}
Output
Key Points
- malloc() allocates memory but does not initialize it, while calloc() allocates and initializes memory to 0.
- realloc() is used to resize a previously allocated memory block.
- Always check the return value of memory allocation functions to ensure that memory was successfully allocated.
- free() is used to deallocate dynamically allocated memory. Failing to free memory leads to memory leaks.
- Dynamic memory allocation is essential when working with large data or when the size of data structures is not known beforehand.