One-dimensional Array Dynamic Memory Allocation
First, let's start with a simple dynamic memory allocation.
#include "stdio.h" #include "stdlib.h" #define N 5 int main() { int* arr = (int*)malloc(sizeof(int)*N); for(int i = 0; i < N; i++) { arr[i] = i * i; } for(int i = 0; i < N; i++) { printf("\n%d", arr[i]); } free(arr); arr = NULL; for(int i = 0; i < N; i++) { printf("\n%d", arr[i]); } }
Dynamic memory allocation for one-dimensional arrays requires only the required memory size (sizeof(int)*N) to allocate array elements as needed.
2-D Array Dynamic Memory Allocation
First, for a neat two-dimensional array, there are two methods of allocation:
- Allocate all memory space at once
- Allocate all memory space twice
One allocation
Assuming we want to allocate memory space for an array of m*n, we can allocate all memory space at once.
#include "stdio.h" #include "stdlib.h" #define m 5 #define n 4 int main() { int* arr = (int*)malloc(sizeof(int)*m*n); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { arr[i*n + j] = 99; } } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { printf("\n%d", arr[i*n + j]); } } }
The memory allocation of the above two-dimensional array is simple, but it is not very convenient to use, so this method is rarely used.
Secondary Allocation
#include "stdio.h" #include "stdlib.h" #define m 5 #define n 4 int main() { int** arr = (int**)malloc(sizeof(int*)*m); for (int i = 0; i < m; ++i) { arr[i] = (int*)malloc(sizeof(int*)*n); } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { arr[i][j] = 99; } } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { printf("\n%d",arr[i][j]); } } }
First, an array of pointers is allocated, each element in the array is an array pointer, which points to a one-dimensional array. Then, memory space is allocated to the array pointer. Compared with the memory allocation of the previous one-dimensional array, this two-dimensional memory allocation is allocated twice, so the allocated memory is not contiguous.
From the memory allocation method above, we know that we can create an irregular two-dimensional array.
#include "stdio.h" #include "stdlib.h" #define m 5 #define n 4 int main() { int** arr = (int**)malloc(sizeof(int*)*m); arr[0] = (int*)malloc(sizeof(int*)*1); arr[1] = (int*)malloc(sizeof(int*)*2); arr[2] = (int*)malloc(sizeof(int*)*3); arr[3] = (int*)malloc(sizeof(int*)*4); arr[4] = (int*)malloc(sizeof(int*)*5); for (int i = 0; i < 1; i++) { arr[0][i] = 9; } for (int i = 0; i < 2; i++) { arr[1][i] = 9; } for (int i = 0; i < 3; i++) { arr[2][i] = 9; } for (int i = 0; i < 4; i++) { arr[3][i] = 9; } for (int i = 0; i < 5; i++) { arr[4][i] = 9; } for (int i = 0; i < m; ++i) { for (int j = 0; j < i+1; ++j) { arr[i][j] = 99; } } for (int i = 0; i < m; ++i) { for (int j = 0; j < i+1; ++j) { printf("\n%d", arr[i][j]); } } }
Dynamic memory allocation with structs and two-dimensional arrays
#include "stdio.h" #include "stdlib.h" #define m 5 #define n 4 typedef struct { int** sums; int sumsSize; } NumMatrix; int main() { int row = 5; int col[5] = {1, 2, 3, 4, 5}; NumMatrix* ret = (NumMatrix*)malloc(sizeof(NumMatrix)); ret->sums = (int **)malloc(sizeof(int*)*row); for (int i = 0; i < row; ++i) { ret->sums[i] = (int*)malloc(sizeof(int)*col[i]); } for (int i = 0; i < row; ++i) { for (int j = 0; j < col[i]; ++j) { ret->sums[i][j] = 99; } } for (int i = 0; i < row; ++i) { for (int j = 0; j < col[i]; ++j) { printf("\n%d", ret->sums[i][j]); } } }
First, let's look at this code. In a structure, there is a two-dimensional array pointer.
So the order in which we allocate memory should follow these steps:
- Allocate memory for the structure first
- Then allocate memory as previously allocated for a two-dimensional array