I’m working with some really large image cubes that are x * y * z in dimension.
Currently I’ve been dealing with them as such
int ***input = malloc(sizeof(int **)*(lines));
int d;
int i;
for(i = 0 ; i<lines ; i++) {
input[i] = malloc(sizeof(int *)*bands);
for(d = 0 ; d<bands ; d++) {
*input[i][d] = malloc(sizeof(int)*(samples));
}
}
This has worked fine for me, but now I’m rewriting some of the code and would like to be able to pass the array by reference
I thought to do so required me passing such as foo(&input)
where the function looks like:
foo(int ****input) {
*input = malloc(sizeof(int **)*(lines));
int d;
int i;
for(i = 0 ; i<lines ; i++) {
*input[i] = malloc(sizeof(int *)*bands);
for(d = 0 ; d<bands ; d++) {
*input[i][d] = malloc(sizeof(int)*(samples));
}
}
}
However, I appear to receive seg faults after it enters the first for(i...)` loop.
Any suggestions would be very helpful, thank you.
This is fine when input is the pointer to the 3D vector:
When input becomes an int
****: a pointer to the vector pointer, this change is incorrect:You want:
In C,
*x[y]means*(x[y]).A much simpler thing would be to use a local variable:
Also, let’s make a few stylistic adjustments to the original. (Ignoring the lack of malloc failure checking):
I just took out some unnecessary parentheses and changed the sizeof calculation so that instead of repeating
(int **), etc, it is based off the type of the pointer expression being assigned to.