gpt4 book ai didi

c++ - 我的程序在 openSUSE 中出现段错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:00:57 25 4
gpt4 key购买 nike

我有一台笔记本电脑,一个月前安装了 openSUSE 13.2。当我尝试运行此 C++ 程序来打印最短路径时,出现段错误。另一方面,我的代码在安装了 Ubuntu 的计算机上完美运行。

这是我在 Ubuntu 中得到的输出...

rohan@Symantha:~/Dropbox/cprog/Arrayss/2D$ uname -a
Linux Symantha 3.13.0-53-generic #89-Ubuntu SMP Wed May 20 10:34:28 UTC 2015 i686 i686 i686 GNU/Linux
rohan@Symantha:~/Dropbox/cprog/Arrayss/2D$ g++ 16.cpp
rohan@Symantha:~/Dropbox/cprog/Arrayss/2D$ ./a.out
Enter total rows : 4
Enter total cols : 4
Enter array ->
1 0 0 0
1 1 1 0
0 1 1 0
1 1 1 1
Input Ok ..
Enter source row: 0
Enter source col: 0
Enter destination row: 3
Enter destination col: 3
Shortest Path -->>
path : (0, 0) -> (1, 0) -> (1, 1) -> (1, 2) -> (2, 2) -> (3, 2) -> (3, 3)

但是在 openSUSE 中,我得到了这个...

rohan@linux-zdor:~/Dropbox/cprog/Arrayss/2D> uname -a
Linux linux-zdor.site 3.16.7-21-desktop #1 SMP PREEMPT Tue Apr 14 07:11:37 UTC 2015 (93c1539) x86_64 x86_64 x86_64 GNU/Linux
rohan@linux-zdor:~/Dropbox/cprog/Arrayss/2D> g++ 16.cpp
rohan@linux-zdor:~/Dropbox/cprog/Arrayss/2D> ./a.out
Enter total rows : 4
Enter total cols : 4
Enter array ->
1 0 0 0
1 1 1 0
0 1 1 0
1 1 1 1
Input Ok ..
Enter source row: 0
Enter source col: 0
Enter destination row: 3
Enter destination col: 3
Shortest Path -->>
Segmentation fault

谁能告诉我我的代码有什么问题吗?这是我的代码:

https://drive.google.com/file/d/0BzLbrlYg9GyXd3E4WnFWc3lGVUk/view?usp=sharing

#include <iostream>
#include <unistd.h>
using namespace std;

#define MAXROW 10
#define MAXCOL 10

class Cell {
public:
int row;
int col;
Cell() { }
Cell(int rr, int cc) : row(rr), col(cc) { }
bool operator==(const Cell& cc) {
if(row == cc.row && col == cc.col)
return true;
else
return false;
}
};

class Path {
int row[MAXROW*MAXROW];
int col[MAXCOL*MAXCOL];
int size;
public:
Path() : size(0) { }
Path(Path& pp) {
size = pp.size;
for(int i = 0; i < size; i++) {
row[i] = pp.row[i];
col[i] = pp.col[i];
}
}
Path& operator=(const Path& pp) {
size = pp.size;
for(int i = 0; i < size; i++) {
row[i] = pp.row[i];
col[i] = pp.col[i];
}
}
int length() {
return size;
}
void setSize(int ss) {
size = ss;
}
void insert(int rr, int cc) {
row[size] = rr;
col[size] = cc;
size++;
}
void print() {
int i = 0;
cout << "path : ";
while(i < size) {
cout << " (" << row[i] << ", "
<< col[i] << ") ";
if(i < size-1)
cout << "-> ";
i++;
}
cout << endl;
}
bool searchCell(Cell c) {
int i = 0;
while(i < size) {
if(row[i] == c.row && col[i] == c.col)
return true;
i++;
}
return false;
}
Cell start() {
return Cell(row[0], col[0]);
}
Cell end() {
if(size > 0)
return Cell(row[size-1], col[size-1]);
else
return Cell(-1, -1);
}
};


Path shortestPath(int arr[][MAXCOL], int, int, Cell, Cell, Path);
void inputArr(int arr[][MAXCOL], int mR, int mC);


int main() {
int arr[10][10], mR, mC, r, c;
Path p;
cout << "Enter total rows : ";
cin >> mR;
cout << "Enter total cols : ";
cin >> mC;
cout << "Enter array ->\n";
inputArr(arr, mR, mC);
cout << "Input Ok ..\n";
cout << "Enter source row: ";
cin >> r;
cout << "Enter source col: ";
cin >> c;
Cell source(r, c);
cout << "Enter destination row: ";
cin >> r;
cout << "Enter destination col: ";
cin >> c;
Cell destination(r, c);
cout << "Shortest Path -->>\n";
p = shortestPath(arr, mR, mC, source, destination, p);
p.print();
return 0;
}

Path shortestPath(int arr[][MAXCOL], int mR, int mC, Cell current, Cell target, Path p) {
int i = current.row;
int j = current.col;
Path p_array[4];
if(arr[i][j] == 0 || i == mR || j == mC || i < 0 || j < 0
|| p.searchCell(current)) {
p.setSize(0);
return p;
}
p.insert(i, j);
if(i == target.row && j == target.col)
return p;
else {
// Since there are four possible directions
p_array[0] = shortestPath(arr, mR, mC, Cell(i, j+1), target, p);
p_array[1] = shortestPath(arr, mR, mC, Cell(i+1, j), target, p);
p_array[2] = shortestPath(arr, mR, mC, Cell(i, j-1), target, p);
p_array[3] = shortestPath(arr, mR, mC, Cell(i-1, j), target, p);
int minIndex, minSize, i;
for(i = 0; i < 4; i++) {
if(p_array[i].length() != 0 && p_array[i].end() == target ) {
minIndex = i;
minSize = p_array[i].length();
break;
}
}
for(i = 0; i < 4; i++) {
if(p_array[i].length() < minSize && p_array[i].end() == target) {
minIndex = i;
minSize = p_array[i].length();
}
}
return p_array[minIndex];
}
}

void inputArr(int arr[][MAXCOL], int mR, int mC) {
int i, j;
for(i = 0; i < mR; i++)
for(j = 0; j < mC; j++)
cin >> arr[i][j];
}

最佳答案

您的 shortestPath 函数中的验证顺序错误:

if (arr[i][j] == 0 || i == mR || j == mC || i < 0 || j < 0 
|| p.searchCell(current)) {

在检查边界是否有效之前,您访问了数组越界。这是未定义的行为,并且不同的系统在存在未定义的行为时可能表现不同(因此崩溃和看似工作都是对调用未定义行为的有效响应)。

在访问数组之前重写条件以检查索引是否正确。

if (i == mR || j == mC || i < 0 || j < 0 || arr[i][j] == 0
|| p.searchCell(current)) {

这可能不是唯一的问题;这是一个问题。

<小时/>

当我从问题中逐字获取代码并编译它时,我收到编译警告(因为我使用 -Werror 将警告转换为错误,所以出现错误)。

$ g++ -O3 -g -std=c++11 -Wall -Wextra -Werror 16.cpp -o 16
16.cpp: In member function ‘Path& Path::operator=(const Path&)’:
16.cpp:41:3: error: no return statement in function returning non-void [-Werror=return-type]
}
^
16.cpp: In function ‘Path shortestPath(int (*)[10], int, int, Cell, Cell, Path)’:
16.cpp:143:40: error: ‘minSize’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
if(p_array[i].length() < minSize && p_array[i].end() == target) {
^
16.cpp:29:15: error: ‘minIndex’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
size = pp.size;
^
16.cpp:134:9: note: ‘minIndex’ was declared here
int minIndex, minSize, i;
^
cc1plus: all warnings being treated as errors
$

这些都是严重的问题,可能会导致您的代码失控。当然,您需要先解决这些问题,然后才值得做其他事情。

我注意到你有:

class Path {
int row[MAXROW*MAXROW];
int col[MAXCOL*MAXCOL];

目前,MAXROW == MAXCOL,因此 rowcol 中的元素数量相同,但如果数字不同,您可能无法将路径的所有成员存储在两个数组的一个或另一个中。不太好。数组的大小应无条件相同。另外,在 main() 中,您定义:

 int arr[10][10], …

当然应该是:

int arr[MAXROW][MAXCOL], …

使用最少的修正,在 shortestPath() 中设置 minIndex = -1minSize = -1,并将 return *this; 添加到赋值运算符,然后在 shortestPath() 末尾添加诊断打印:

Path shortestPath(int arr[][MAXCOL], int mR, int mC, Cell current, Cell target, Path p) {
int i = current.row;
int j = current.col;
Path p_array[4];
cerr << "-->> shortestPath(" << i << "," << j << ")\n";
//if(arr[i][j] == 0 || i == mR || j == mC || i < 0 || j < 0
if (i == mR || j == mC || i < 0 || j < 0 || arr[i][j] == 0 || p.searchCell(current)) {
p.setSize(0);
cerr << "<<-- shortestPath() - 1\n";
return p;
}
p.insert(i, j);
if (i == target.row && j == target.col)
{
cerr << "<<-- shortestPath() - 2\n";
return p;
}
else {
// Since there are four possible directions
p_array[0] = shortestPath(arr, mR, mC, Cell(i, j+1), target, p);
p_array[1] = shortestPath(arr, mR, mC, Cell(i+1, j), target, p);
p_array[2] = shortestPath(arr, mR, mC, Cell(i, j-1), target, p);
p_array[3] = shortestPath(arr, mR, mC, Cell(i-1, j), target, p);
int minIndex = -1, minSize = -1, i;
for(i = 0; i < 4; i++) {
if(p_array[i].length() != 0 && p_array[i].end() == target ) {
minIndex = i;
minSize = p_array[i].length();
cerr << "1: minIndex: " << minIndex << ", minSize: " << minSize << "\n";
break;
}
}
for(i = 0; i < 4; i++) {
if(p_array[i].length() < minSize && p_array[i].end() == target) {
minIndex = i;
minSize = p_array[i].length();
cerr << "2: minIndex: " << minIndex << ", minSize: " << minSize << "\n";
}
}
cerr << "3: minIndex: " << minIndex << "\n";
cerr << "<<-- shortestPath() - 3\n";
return p_array[minIndex];
}
}

并运行它,部分输出为:

-->> shortestPath(2,0)
<<-- shortestPath() - 1
3: minIndex: -1
<<-- shortestPath() - 3

糟糕:访问和返回 p_array[-1] 不是一个好主意。你的下标失去了控制——难怪事情会变得困惑。您需要思考代码是否将 minIndex 设置为 -1 以外的值以及如何处理它。

关于c++ - 我的程序在 openSUSE 中出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31052197/

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