在我的代码中,我定义了一个向量:
vector<vector<vector<vector<shared_ptr<foo> > > > > fooBoxes;
我正在使用:
初始化矢量int BOX_NUM = 12 //this is actually defined elsewhere
fooBoxes.resize(BOX_NUM);
for (int i = 0; i<BOX_NUM; i++){
fooBoxes[i].resize(BOX_NUM);
for (int j = 0; j < BOX_NUM; j++){
fooBoxes[i][j].resize(BOX_NUM);
for (int k = 0; k < BOX_NUM; k++){
fooBoxes[i][j][k].resize(0);
}
}
}
我怀疑使用vector导致我出现分段错误,我想将fooBoxes
替换为:
vector<vector<vector<set<shared_ptr<foo> > > > > fooBoxes
我在for
循环中该怎么办?只需删除resize(0)
部分?
修改
这是崩溃中valgrind的输出:
==2258== Invalid read of size 8
==2258== at 0x439237: trans(int) (stl_iterator.h:704)
==2258== by 0x413B0E: membrane::MCstep(int) (membrane.cpp:490)
==2258== by 0x402767: main (main.cpp:14)
==2258== Address 0x7932420 is 8 bytes after a block of size 24 free'd
==2258== at 0x4A05743: operator delete(void*) (vg_replace_malloc.c:346)
==2258== by 0x405636: vec::~vec() (valarray_array.h:71)
==2258== by 0x437D66: trans(int) (transFile.cpp:64)
==2258== by 0x413B0E: membrane::MCstep(int) (membrane.cpp:490)
==2258== by 0x402767: main (main.cpp:14)
==2258==
==2258== Invalid read of size 8
==2258== at 0x439240: trans(int) (stl_vector.h:604)
==2258== by 0x413B0E: membrane::MCstep(int) (membrane.cpp:490)
==2258== by 0x402767: main (main.cpp:14)
==2258== Address 0x111 is not stack'd, malloc'd or (recently) free'd
==2258==
==2258==
==2258== Process terminating with default action of signal 11 (SIGSEGV)
==2258== Access not within mapped region at address 0x111
==2258== at 0x439240: trans(int) (stl_vector.h:604)
==2258== by 0x413B0E: membrane::MCstep(int) (membrane.cpp:490)
==2258== by 0x402767: main (main.cpp:14)
==2258== If you believe this happened as a result of a stack
==2258== overflow in your program's main thread (unlikely but
==2258== possible), you can try to increase the size of the
==2258== main thread stack using the --main-stacksize= flag.
==2258== The main thread stack size used in this run was 10485760.
我认为问题在于,当我尝试删除/将珠子放入矢量时,我不够小心,这就是我想要设置的原因。
答案 0 :(得分:1)
这取决于你想要达到的目标。如果删除fooBoxes[i][j][k].resize(0);
,您的代码肯定会编译,因为std::set
不支持此功能。