How to write a c program to dynamic memory allication for 2d array in c programming (Example) ?
Dynamically allocated 2D arrays
Dynamically declared 2D arrays can be allocated in one of two ways. For a NxM 2D array:- Allocate a single chunk of NxM heap space
- Allocate an array of arrays: allocate 1 array of N pointers to arrays, and allocate N M bucket array of values (on for each row). You could also allocate each column independently and have an array of column arrays.
// (1) a single NxM malloc: really this is a large 1-dim array of int values
// onto which we will map 2D accesses
//
// declaration and allocation:
int *2d_array; // the type is a pointer to an int (the bucket type)
2d_array = (int *)malloc(sizeof(int)*N*M);
// in memory:
// row 0 row 1 row 2 ...
// 2d_array -----> [ 0, 0, ... , 0, 0, ... 0, 0, ... ]
// access using [] notation:
// cannot use [i][j] syntax because the compiler has no idea where the
// next row starts within this chunk of heap space, so must use single
// index value that is calculated using row and column index values and
// the column dimension
for(i=0; i < N; i++) {
for(j=0; j < M; j++) {
2d_array[i*M +j] = 0;
}
}
// access using pointer arithmetic (all N*M buckets are contiguous)
int *ptr = 2d_array;
for(i=0; i < N; i++) {
for(j=0; j < M; j++) {
*ptr = 0;
ptr++;
}
}
// (2) N mallocs, one for each row, plus one malloc for array of row
// arrays. The bucket locations in individual rows are contiguous,
// but rows are not necessarily contiguous in heap space.
//
int **2d_array; // an array of int arrays (a pointer to pointers to ints)
// declaration and allocation:
// allocate an array of N pointers to ints
// malloc returns the address of this array (a pointer to (int *)'s)
2d_array = (int **)malloc(sizeof(int *)*N);
// for each row, malloc space for its buckets and add it to
// the array of arrays
for(i=0; i < N; i++) {
2d_array[i] = (int *)malloc(sizeof(int)*M);
}
// in memory:
// 2d_array -----> | *-|-------> [ 0, 0, 0, ... , 0] row 0
// | *-|-------> [ 0, 0, 0, ... , 0] row 1
// |...| ...
// | *-|-------> [ 0, 0, 0, ... , 0]
// access using [] notation:
// 2d_array[i] is ith bucket in 2d_array, which is the address of
// a 1d array, on which you can use indexing to access its bucket value
for(i=0; i < N; i++) {
for(j=0; j < M; j++) {
2d_array[i][j] = 0;
}
}
// access using pointer arithmetic (NOT all N*M buckets are contigous)
// but each row's buckets are
int *ptr;
for(i=0; i < N; i++) {
*ptr = 2d_array[i]; // set pointer to address of bucket 0 in row i
for(j=0; j < M; j++) {
*ptr = 0;
ptr++;
}
}
Solution for Dynamic Memory Allocation For 2D Array:
#include <stdio.h>#include <stdlib.h>
void swap(int *a, int *b);
int main()
{
int **x, n, i, j;
printf("How many numbers?\n");
scanf("%d", &n);
x = (int **) malloc(n*sizeof(int *));
for(i = 0; i < n; i++)
x[i] = (int *) malloc((i+1)*sizeof(int));
for(i = 0; i < n; i++)
for(j = 0; j <= i; j++)
x[i][j] = i + j;
for(i = 0; i < n; i++)
{
for(j = 0; j <= i; j++)
printf("%d ", x[i][j]);
printf("\n");
}
for(i = 0; i < n; i++)
free(x[i]);
free(x);
return 0;
}
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}