Im writing a little thing involving voxels, so I have a 3d array filled with data, like this
typedef struct data{float m,vx,vy,vz;} data;
data pool[2][x][y][z];
data voronoi[26];
where x
, y
and z
are uint64_t
. But their actual value depends on user input during runtime. Now it turns out that I get a segmentation fault when the array gets too big (can't even do 128x128x128), even though I have more than enough RAM. So I need to allocate it on the heap. How do I do this, while also not needing to convert code like this:
uint64_t h,b,d,p;
p = 0;
for(h=1;h<y-1;++h){
for(b=1;b<x-1;++b){
for(d=1;d<z-1;++d){
voronoi[ 0] = pool[p][h-1][b-1][d-1];
voronoi[ 1] = pool[p][h ][b-1][d-1];
voronoi[ 2] = pool[p][h+1][b-1][d-1];
voronoi[ 3] = pool[p][h-1][b ][d-1];
voronoi[ 4] = pool[p][h ][b ][d-1];
voronoi[ 5] = pool[p][h+1][b ][d-1];
voronoi[ 6] = pool[p][h-1][b+1][d-1];
voronoi[ 7] = pool[p][h ][b+1][d-1];
voronoi[ 8] = pool[p][h+1][b+1][d-1];
//etc until 26
}}}
p = 1;
// repeat cycle, then p=0 again, etc
Simply malloc
ing pool like so:
data* pool=(2*x*y*z*sizeof(data));
This is not a duplicate, because malloc
-ing a 1d array and adding custom indexing (the goto answer) invalidates the multidimensional square bracket indexing syntax.