【发布时间】:2017-11-09 16:24:14
【问题描述】:
我在运行代码时收到了段错误。该代码包含一个 for 循环,当我运行该程序进行较小的迭代时,不会出现分段错误。当我为更大的循环运行代码时它会出现。 我使用调试器检查问题发生在哪里:
Program received signal SIGSEGV, Segmentation fault.
0x00000000004062d0 in std::vector<int, std::allocator<int> >::push_back (
this=0x64dc08, __x=@0x7fffffffd180: 32)
at /usr/include/c++/5/bits/stl_vector.h:915
915 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
这是什么意思?有人知道吗?代码错误与这部分代码有关:
int box_new = getBoxID(x[i],y[i],R0,nbox);
if(bx[i] != box_new)
{
vector<int>::iterator position = std::find(box_particles[bx[i]].begin(), box_particles[bx[i]].end(), i);
if (position != box_particles[bx[i]].end()) // .end() means the element was not found
box_particles[bx[i]].erase(position);
bx[i] = box_new;
box_particles[box_new].push_back(i);
}
【问题讨论】:
-
代码包含一个 for 循环我们非常想看到这个循环。
-
发布您的代码可能会有所帮助。但看起来您正在尝试访问超过矢量的大小。
-
我不知道是代码的哪一部分。如何找到故障线? @kjpus
-
我已经添加了代码的相关部分@GauravSehgal
-
分而治之。从程序中删除不需要触发错误的所有内容。基本上,制作一个minimal reproducible example。