This question is in reference to the solution/explanation provided here Allocating variable length multi dimensional arrays ( see the accepted answer).
In my case I am using the same function for defining a 3D array like following
void arr_alloc (int x, int y, int z, int(**aptr)[x][y][z])
{
*aptr = malloc( sizeof(int[x][y][z]) ); // allocate a true 3D array
assert(*aptr != NULL);
}
For the codebase I am working on, which is a huge project repository, earlier I was using pointers to pointers to pointers to type (***array) and then interleaving such that I can use it as a 3D (pseudo)array. For this I used to declare the variable as extern int ***array
and define the pointer in a separate header file as int ***array
. My question - (a) For the new function in place what should be the declaration and definition for the array given that the SO reference answer uses the declaration and definition such as
int x = 2;
int y = 3;
int (*aptr)[x][y];
(b) Incentive of using size_t
vs int
as the indexing variable given that size_t
occupies 8 bytes whereas int
occupies 4 bytes.
PS. I checked in gdb the variable type after the declaration and definition line mentioned above (also in the SO answer) which is (int (*)[variable length][variable length][variable length]) 0x0
Edit : Please note that this question is about the declaration and definition of the array / array pointer and not the declaration of the function that allocates and sets up the array in place.