如何处理数组C++中的分段错误
How to handle segmentation fault in an array C++
我正在创建一个程序,该程序基于跟踪顶点信息以及从一个顶点到另一个顶点的最短路径的集合和表格来查找从一个节点到另一顶点的最长路径。这是使用数组而不是链表创建的。
//--------------------------------------------------------------------------
// 'updatepaths' uses the newest vertex which was added to the Set to modify
// the distances of the remaining vertices (if smaller)
// in addition to the newly added vertex, it uses the Set, the Vertexinfo
// and the Shortpath tables
//--------------------------------------------------------------------------
void Paths::updatepaths(int addnode)
{
if (done.in(addnode)) //done is a set instance, checks if the value is in the set
{
for (int i = 0; i<VERTICES; i++)
{
if (shortpath[edgedata[addnode].path[i].vertexto].distance > edgedata[addnode].path[i].weight) //HERE IS THE ISSUE
{
shortpath[edgedata[addnode].path[i].vertexto].distance = edgedata[addnode].path[i].weight;
shortpath[edgedata[addnode].path[i].vertexto].via = addnode;
}
}
}
}
我意识到代码很难阅读,但这是我能想到的唯一比较顶点距离的方法——问题是,在if语句中,有时它会尝试比较数组中不存在的值。
例如,edgedata[addnode].path[0].weight
可能包含NO VALUE,因此我的程序会抛出访问违规(分段错误)。我尝试在if语句中设置edgedata[addnode].path[i].weight != NULL
和0,但在算术过程中不能使用NULL,如果它不存在,就永远不会是0。
我应该如何制作它,这样它就不会试图比较不存在的值?谢谢你的帮助。
如果您的逻辑经常碰到NULL对象,代码中可能会出现更大的设计或实现问题,但要解决眼前的问题,最简单的方法是使用std::array<>::at()
而不是std::array<>::operator[]
,并捕获它可以生成的out_of_range
异常:
try {
if (shortpath.at(edgedata.at(addnode).path.at(i).vertexto).distance >
edgedata.at(addnode).path.at(i).weight)
{
shortpath.at(edgedata.at(addnode).path.at(i).vertexto).distance =
edgedata.at(addnode).path.at(i).weight;
shortpath.at(edgedata.at(addnode).path.at(i).vertexto).via = addnode;
}
}
catch (std::out_of_range const &oor) {
std::cerr << "Out of Range error: " << oor.what() << std::endl;
}
或者,您可以在if语句中沿着以下行缩短检查(我可能错过了一两次检查,所以请注意):
if ((edgedata.size() >= addnode)
&& (edgedata[addnode].path.size() >= i)
&& (shortpath.size() >= edgedata[addnode].path[i].vertexto)
&& (shortpath[edgedata[addnode].path[i].vertexto].distance >
edgedata[addnode].path[i].weight))
{
shortpath[edgedata[addnode].path[i].vertexto].distance =
edgedata[addnode].path[i].weight;
shortpath[edgedata[addnode].path[i].vertexto].via = addnode;
}
编写c++代码时,应首先使用c++编码风格。例如,你可以使用std::vector
而不是数组,那么你应该使用iterator
来访问向量的元素,或者使用vec.at(i)
,因为这两种方法可以检查超出的边界,在c中,它没有办法这样做
相关文章:
- 分段故障(堆芯转储)矢量
- 数组的指针从不分段故障
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 在线编译器中的分段C++没有打印消息
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- 创建结构的数组时遇到分段错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 在 c++ 中实现 Trie 时出现分段错误
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 为什么我遇到分段错误?
- 动态类的分段错误(家庭作业问题)