gpt4 book ai didi

c++ - Ubuntu 机器上的段错误,但 Mac 上没有

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:47:20 25 4
gpt4 key购买 nike

我在 Mac 机器上做作业,代码编译后我得到了预期的输出。一旦我去运行程序进行评分的 Ubuntu 机器上对其进行测试,我就会收到段错误(核心转储)错误消息。我真的很困惑,正在寻找一些技巧来调试为什么在 ubuntu 机器(版本 12.04)而不是 mac 机器(mavericks OS)上发生这种情况。

编辑:通过 gdb 运行它,我在

得到了段错误
in getPath: line 28, if (path[position] == 0 && position == 0) {

我的代码如下:

#include <iostream>

using namespace std;

int minDistance(int distance[], bool shortestPath[]) {

int min = 1000000000;
int indexOfMin;

for (int i = 0; i < 6; i++) {
if (shortestPath[i] == false && distance[i] <= min) {
min = distance[i];
indexOfMin = i;
}
}

return indexOfMin;
}

void getPath(int path[], int position) {
cout << position + 1 << " <- ";

// base case, we hit the end of the path
if (path[position] == 0 && position == 0) {
cout << path[position] + 1 << endl;
}
else if (path[position] == 0)
cout << path[position] + 1 << endl;
else {
getPath(path, path[position]);
}
}

void dijkstraAlgorithm(int weightedGraph[6][6], int sourceVertex) {
int distance[6];
bool shortestPath[6];
int path[6];

for (int i = 0; i < 6; i++) {
distance[i] = 1000000000;
shortestPath[i] = false;
}

distance[sourceVertex] = 0;

for (int j = 0; j < 5; j++) {
int min = minDistance(distance, shortestPath);
shortestPath[min] = true;
for (int k = 0; k < 6; k++) {
if (!shortestPath[k] && weightedGraph[min][k] && distance[min] != 1000000000 && distance[min] + weightedGraph[min][k] < distance[k]) {
distance[k] = weightedGraph[min][k] + distance[min];
path[k] = min;
}
}
}

cout << "Distance Path" << endl;
for (int i = 0; i < 6; i++) {
// dist[i] == 0 a formatting fix for first distance
if (distance[i] == 0) {
cout << distance[i] << ": ";
getPath(path, i);
}
else {
cout << distance[i] << ": ";
getPath(path, i);
}
}
}

int main() {
int weightedGraph[6][6] = {
{0, 10, 0, 30, 100, 0},
{0, 0, 50, 0, 0, 0},
{0, 0, 0, 0, 10, 5},
{0, 0, 20, 0, 0, 15},
{0, 0, 0, 60, 0, 0},
{0, 0, 0, 0, 0, 0}
};

dijkstraAlgorithm(weightedGraph, 0);

return 0;
}

注意:我需要在Ubuntu环境下使用g++编译器(版本4.6.4)

最佳答案

valgrind 下运行您的示例代码显示这些 uninitialized read错误:

==25442== Conditional jump or move depends on uninitialised value(s)
==25442== at 0x4008B2: getPath(int*, int) (crash.cpp:24)
==25442== by 0x400B19: dijkstraAlgorithm(int (*) [6], int) (crash.cpp:62)
==25442== by 0x400B9C: main (crash.cpp:81)

和:

==25442== Conditional jump or move depends on uninitialised value(s)
==25442== at 0x4008F8: getPath(int*, int) (crash.cpp:27)
==25442== by 0x400B19: dijkstraAlgorithm(int (*) [6], int) (crash.cpp:62)
==25442== by 0x400B9C: main (crash.cpp:81)

等等。

不要假设这些数组将自动初始化为零 (0):

int distance[6];
bool shortestPath[6];
int path[6];

初始化这些数组,您将消除一些问题(至少)。

引用Initialization of a normal array with one default value .

已更新:为什么它可以在 Mac 上运行很可能只是巧合,如对 Turn off automatic initialization of variables in Xcode

关于c++ - Ubuntu 机器上的段错误,但 Mac 上没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20389714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com