I have a struct Node and Box given by
typedef struct Node{ Particle p; Box box; struct Node *son[4]; }Node and
typedef struct Box{ double low[3]; double up[3]; }Box I have two functions insert() and sonumb() where I want to use these structures.
void insert(Particle *p, Node *t){ Box sonbox; int b=sonumb(&t->box, &sonbox, p); t->son[b]->box = sonbox; // <--- Produces Segmentation fault (core dumped) } int sonumb(Box *box, Box *sonbox, Particle *p){ int b=0; for(int d=0;d<3;d++){ sonbox->up[d] = 0.5*box->up[d]; sonbox->low[d] = 0.5*box->low[d]; } b=1; // b=[0,3] just for this example } sonum() returns an integer value b. sonbox represents after the call of sonumb() a smaller box inside of t->box. I return the right values for sonbox after the call. So sonbox is not empty. But if I want to copy those values like t->son[b]->box = sonbox I get an segmentatioin fault. What did I miss?
sonboxis not empty either way. Possible causes for a segfault areb < 0 || b > 3ort->son[b]being uninitialized.sonis struct pointer array and you need to allocate memory withmallocbefore you assign box each elementsson[b]mostly likely contains/evaluates to garbage. But we do not know, as you do not show the relevant code.t->son[b].