#include <stdio.h>
#include <stdlib.h>
void* create2dArray(int r,int c) {
int (*arr)[c] = (int(*)[c])malloc(sizeof(*arr) * r);
return arr;
}
void release2dArray(void* arr) {
free(arr);
}
int main() {
int r = 10, c = 20;
int (*arr)[c] = (int (*)[c])create2dArray(r, c);
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
arr[i][j] = i * c + j;
printf("%d ", arr[i][j]);
}
printf("\n");
}
release2dArray(arr);
arr[0][2] = 20;
printf("%d ", arr[1][2]);
}
In the code, I allocate contiguous memory for a 2D array using a pointer to the array. To release the memory, I want to directly use release2dArray(arr) because I know that the memory allocated information is stored somewhere before the allocated memory itself. I think it should work but it fails. So is it possible for me to release the memory of 2D array without passing the shape of that array into function release2dArray() and without using a for loop?
"However, if releasing is successfully, I think all elements should be 0 except the arr[0][2]."
No, do not expect that. After release2dArray(arr);
there is no defined way to use arr
with its old value.. Do not attempt to access via the pointer after free()
, unless it is later assigned allocated memory.
release2dArray(arr);
// Do not attempt the following code.
// It is undefined behavior (UB).
//arr[0][2] = 20;
//printf("%d ", arr[1][2]);
is it possible for me to release the memory of 2D array without passing the shape of that array into function release2dArray() and without using a for loop?
Yes. This part of OP's code is fine.
void release2dArray(void* arr) {
free(arr);
}
...
release2dArray(arr); // Just fine
BTW, OP is allocating well.
Minor: The cast is not needed.
Minor: Consider size_t
for array sizing. That type is neither too narrow nor too wide.
// void* create2dArray(int r, int c) {
void* create2dArray(size_t r, size_t c) {
// int (*arr)[c] = (int(*)[c])malloc(sizeof(*arr) * r);
int (*arr)[c] = malloc(sizeof *arr * r);
return arr;
}