我的程序在openSUSE中给出了分段错误

时间:2015-06-25 13:45:59

标签: c++ linux

我有一台笔记本电脑,我在一个月前安装了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];
}

1 个答案:

答案 0 :(得分:0)

您的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], …

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

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以外的值以及如何处理它。