У меня есть ноутбук, на котором я установил openSUSE 13.2 месяц назад. Когда я пытаюсь запустить эту программу на C++ для печати кратчайшего пути, я получаю ошибку сегментации. С другой стороны, мой код отлично работает на моем компьютере, на котором установлен Ubuntu.Моя программа дает ошибку сегментации в openSUSE
Вот результат я получаю в Ubuntu ...
[email protected]:~/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
[email protected]:~/Dropbox/cprog/Arrayss/2D$ g++ 16.cpp
[email protected]:~/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, я получаю это ...
[email protected]:~/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
[email protected]:~/Dropbox/cprog/Arrayss/2D> g++ 16.cpp
[email protected]:~/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];
}
запустить отладчик, проверить, где он выбрасывает – Hcorg
Это не так, как работает Stackoverflow. Прочтите [ask] информацию о том, как задать хороший вопрос. – dandan78
Прошу прощения, это мой первый раз. –