Does anyone know why this would give me a segmentation fault?
cell.h
struct cell{
bool filled;
bool isParent;
//float px,py,pz,s;
bool cx,cy,cz;
unsigned char r,g,b;
vect norm;
struct cell* parent;
struct cell* child;
cell(bool cxx=0, bool cyy=0, bool czz=0);
void open_read(string);
};
cell.cpp
cell::cell(bool cxx, bool cyy, bool czz)
{
cell childs[8]; // these lines creates a segmentation fault
child = &childs[0]; // these lines creates a segmentation fault
cx=cxx;
cy=cyy;
cz=czz;
norm = vect(0,0,0);
norm.normalize();
isParent=false;
filled=true;
}
If this is the wrong way to do this could anyone point me in the right direction as to how I could store a single pointer to the first element of child[8] instead of storing 8 pointers as it is quite memory intensive.
You are trying to set up an infinite recursion. The constructor of
cellallocates an array of 8cellobjects, whose construction in turn invokes the constructor ofcellwith default arguments.Each stack frame consumes space, and sooner or later the stack will grow bigger than its size limit due to the non-terminated call recursion, eventually resulting in a segmentation fault.