Dynamic 1-D Array:
Dynamic memory allocation in C allows you to allocate memory at runtime for variables or arrays whose size is determined during program execution. This is particularly useful when you need to create arrays or when the required memory size is unknown at compile time. The dynamic memory allocation functions in C are `malloc`, `calloc`, `realloc`, and `free`, which are declared in the `<stdlib.h>` header.
1. `malloc`:
This function is used to allocate a block of memory of a specified size in bytes. It takes a single argument, which is the number of bytes to be allocated, and returns a pointer to the beginning of the allocated memory block. If the memory allocation fails, it returns a `NULL` pointer.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
// Allocate memory for 5 integers
ptr = (int*)malloc(5 * sizeof(int));
if (ptr == NULL)
{
printf("Memory allocation failed.\n");
return 1;
}
// Access and modify elements of the allocated array
for (int i = 0; i < 5; i++)
{
ptr[i] = i + 1;
printf("%d ", ptr[i]);
}
// Free the dynamically allocated memory
free(ptr);
return 0;
}
2. `calloc`:
The `calloc` function is used to allocate memory for an array of elements, each of a specified size. It takes two arguments: the number of elements to be allocated and the size of each element in bytes. The memory allocated by `calloc` is initialized to zero. Like `malloc`, if the memory allocation fails, it returns a `NULL` pointer.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
// Allocate memory for 5 integers, initialized to zero
ptr = (int*)calloc(5, sizeof(int));
if (ptr == NULL)
{
printf("Memory allocation failed.\n");
return 1;
}
// Access and modify elements of the allocated array
for (int i = 0; i < 5; i++)
{
ptr[i] = i + 1;
printf("%d ", ptr[i]);
}
// Free the dynamically allocated memory
free(ptr);
return 0;
}
3. `realloc`:
The `realloc` function allows you to resize an already dynamically allocated block of memory. It takes two arguments: a pointer to the previously allocated memory block and the new size in bytes. `realloc` returns a pointer to the beginning of the resized memory block. If the resizing fails, it returns a `NULL` pointer. It's important to note that `realloc` may copy the contents of the original memory block to a new location if necessary.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
// Allocate memory for 5 integers
ptr = (int*)malloc(5 * sizeof(int));
if (ptr == NULL)
{
printf("Memory allocation failed.\n");
return 1;
}
// Access and modify elements of the allocated array
for (int i = 0; i < 5; i++)
{
ptr[i] = i + 1;
printf("%d ", ptr[i]);
}
// Resize the array to hold 10 integers
ptr = (int*)realloc(ptr, 10 * sizeof(int));
if (ptr == NULL)
{
printf("Memory reallocation failed.\n");
return 1;
}
// Access and modify elements of the resized array
for (int i = 5; i < 10; i++)
{
ptr[i] = i + 1;
printf("%d ", ptr[i]);
}
// Free the dynamically allocated memory
free(ptr);
return 0;
}
It's important to remember that dynamically allocated memory should be released with `free` when it is no longer needed. Failure to do so may result in memory leaks, where memory that is no longer in use is not freed, leading to inefficient memory usage. Additionally, accessing memory after it has been freed leads to undefined behavior, and such dangling pointers should be avoided. (alert-success)
Dynamic 2-D Array:
To create a 2D dynamic array in C, you need to use pointers to pointers (also known as double pointers). This approach involves first creating an array of pointers, where each pointer points to a separate 1D array representing a row of the 2D array.
#include <stdio.h>
#include <stdlib.h>
int main() {
int numRows = 3;
int numCols = 4;
int **twoDArray;
// Allocate memory for the array of pointers (rows)
twoDArray = (int**)malloc(numRows * sizeof(int*));
// Allocate memory for each row (1D array) and assign the address to the corresponding pointer
for (int i = 0; i < numRows; i++) {
twoDArray[i] = (int*)malloc(numCols * sizeof(int));
}
// Assign values to the elements of the 2D array
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
twoDArray[i][j] = i * numCols + j;
}
}
// Print the elements of the 2D array
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
printf("%d ", twoDArray[i][j]);
}
printf("\n");
}
// Free the allocated memory
for (int i = 0; i < numRows; i++) {
free(twoDArray[i]);
}
free(twoDArray);
return 0;
}