有谁知道为什么这会给我一个分段错误?这个例子为什么会出现分段错误?
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;
}
如果这是错误的方式做到这一点任何人都可以点我在正确的方向如何,我可以存储一个指针到孩子的第一个元素[8],而不是存储8个指针,因为它是相当密集的内存。
如果它最终会停止,它并不是真正的无限......尽管这是一种不正经的态度。 – 2013-02-10 16:31:47
@SteveWellens:对,我的意思是说他正在试图建立一个。但你是对的。我编辑了我的答案。 – 2013-02-10 16:32:28
哦,我应该看到,我想我需要看看我可以如何改变我的程序,所以我不需要在实际需要之前调用childs的构造函数。 – lasvig 2013-02-10 16:32:42