Wednesday, 15 January 2014

c++ - Why would this example give a segmentation fault? -



c++ - Why would this example give a segmentation fault? -

does know why give me 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 segmentation fault kid = &childs[0]; // these lines creates segmentation fault cx=cxx; cy=cyy; cz=czz; norm = vect(0,0,0); norm.normalize(); isparent=false; filled=true; }

if wrong way point me in right direction how store single pointer first element of child[8] instead of storing 8 pointers quite memory intensive.

you trying set infinite recursion. constructor of cell allocates array of 8 cell objects, construction in turn invokes constructor of cell default arguments.

each stack frame consumes space, , sooner or later stack grow bigger size limit due non-terminated phone call recursion, resulting in segmentation fault.

c++ pointers segmentation-fault

No comments:

Post a Comment