As others have said, it is probably better to allocate one contiguous chunk of memory, and then figure out the indexing yourself. You can write a function to do so if you want. But since you seem to be interested in knowing how to deal with the multiple malloc()
case, here is an example:
First, I define a function free_data()
, which frees an int ***
with xlen
and ylen
as the first two dimension sizes. We don't need a zlen
parameter just like free()
doesn't take the length of the pointer being freed.
void free_data(int ***data, size_t xlen, size_t ylen)
{
size_t i, j;
for (i=0; i < xlen; ++i) {
if (data[i] != NULL) {
for (j=0; j < ylen; ++j)
free(data[i][j]);
free(data[i]);
}
}
free(data);
}
The function loops over the pointer data
, finds out the i
th int **
pointer data[i]
. Then, for a given int **
pointer, it loops over it, finding out the j
th int *
in data[i][j]
, and frees it. It also needs to free data[i]
once it has freed all data[i][j]
, and finally, it needs to free data
itself.
Now to the allocation function. The function is a bit complicated by error checking. In particular, since there are 1 + xlen + xlen*ylen
malloc
calls, we have to be able to handle a failure in any of those calls, and free all the memory we allocated so far. To make things easier, we rely on the fact that free(NULL)
is no-op, so we set all the pointers at a given level equal to NULL
before we try to allocate them, so that if an error happens, we can free all of the pointers.
Other than that, the function is simple enough. We first allocate space for xlen
int **
values, then for each of those xlen
pointers, we allocate space for ylen
int *
values, and then for each of those xlen*ylen
pointers, we allocate space for zlen
int
values, giving us a total space for xlen*ylen*zlen
int
values:
int ***alloc_data(size_t xlen, size_t ylen, size_t zlen)
{
int ***p;
size_t i, j;
if ((p = malloc(xlen * sizeof *p)) == NULL) {
perror("malloc 1");
return NULL;
}
for (i=0; i < xlen; ++i)
p[i] = NULL;
for (i=0; i < xlen; ++i)
if ((p[i] = malloc(ylen * sizeof *p[i])) == NULL) {
perror("malloc 2");
free_data(p, xlen, ylen);
return NULL;
}
for (i=0; i < xlen; ++i)
for (j=0; j < ylen; ++j)
p[i][j] = NULL;
for (i=0; i < xlen; ++i)
for (j=0; j < ylen; ++j)
if ((p[i][j] = malloc(zlen * sizeof *p[i][j])) == NULL) {
perror("malloc 3");
free_data(p, xlen, ylen);
return NULL;
}
return p;
}
Note that I have simplified malloc
calls quite a bit: in general, you shouldn't cast the return value of malloc
, and specify the object you're allocating for as the operand to sizeof
operator instead of its type. That makes malloc
calls simpler to write and less error-prone. You need to include stdlib.h
for malloc
.
Here is a test program using the above two functions:
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
int ***data;
size_t xlen = 10;
size_t ylen = 100;
size_t zlen = 300;
size_t i, j, k;
srand((unsigned int)time(NULL));
if ((data = alloc_data(xlen, ylen, zlen)) == NULL)
return EXIT_FAILURE;
for (i=0; i < xlen; ++i)
for (j=0; j < ylen; ++j)
for (k=0; k < zlen; ++k)
data[i][j][k] = rand();
printf("%d\n", data[1][2][1]);
free_data(data, xlen, ylen);
return EXIT_SUCCESS;
}
By all means use this approach if you find it easier to use it. In general, this will be slower than using a contiguous chunk of memory, but if you find that the speed is OK with the above scheme, and if it makes your life easier, you can keep using it. Even if you don't use it, it is nice to know how to make such a scheme work.