Here's my code:
#include <stdlib.h> //malloc
#define lineSize 16
#define columnSize 16
#define cellSize 16
int main()
{
unsigned char*** tab;
tab = malloc(sizeof(unsigned char**) * lineSize);
for(unsigned int i = 0; i < lineSize; i++)
tab[i] = malloc(sizeof(unsigned char*) * columnSize);
for(unsigned int i = 0; i < lineSize; i++)
for(unsigned int j = 0; j < columnSize; j++)
tab[i][j] = malloc(sizeof(unsigned char) * cellSize);
unsigned int line = 0;
unsigned int column = 0;
unsigned int cell = 0;
unsigned char* ptr = &tab[line][column][cell];
for(line = 0; line < lineSize; line++)
for(column = 0; column < columnSize; column++)
for(cell = 0; cell < cellSize; cell++)
*ptr = ...;
return 0;
}
This code fills tab with values that are only known at execution time
In this case there's no much problems because lineSize, columnSize and cellSize are small, The problem is that when cellSize become 100000+ then dereferencing the pointer gets expensive in terms of time, that's why I thought to use a pointer to avoid dereferencing.
The problem is that I don't know how to do in order for the pointer to be updated as line, column or cell change.
Appreciates your help, Thanks.
EDIT: More explanations:
The bigger lineSize, columnSize and cellSize, the longer is the execution. It's expected but what takes a "lot of time" is what's inside the loops and inside the loops is a pointer dereferencing 16*16*100000 times (When cellSize = 100000
).
If I'm right, Dereferencing is a multiplication like:
tab[2][5][3] = tab + 2*16*100000 + 5*100000 + 3;
And do math like that 16*16*100000 times is long.
So to avoid maths, I thought about a pointer that permanently points totab[line][column][cell]
but I don't know how to do it without having to recalculate the pointer each time cell
is incremented.