c++ - My program gives segmentation fault in openSUSE -
i have laptop in i've installed opensuse 13.2 month ago. when try run c++ program print shortest path, segmentation fault. on other hand, code works on computer on ubuntu installed.
here output in 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)
but in opensuse, this...
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
can tell me wrong code? here code:
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 = 0; < size; i++) { row[i] = pp.row[i]; col[i] = pp.col[i]; } } path& operator=(const path& pp) { size = pp.size; for(int = 0; < 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 = 0; cout << "path : "; while(i < size) { cout << " (" << row[i] << ", " << col[i] << ") "; if(i < size-1) cout << "-> "; i++; } cout << endl; } bool searchcell(cell c) { int = 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 = current.row; int j = current.col; path p_array[4]; if(arr[i][j] == 0 || == mr || j == mc || < 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 4 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; < 4; i++) { if(p_array[i].length() != 0 && p_array[i].end() == target ) { minindex = i; minsize = p_array[i].length(); break; } } for(i = 0; < 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; < mr; i++) for(j = 0; j < mc; j++) cin >> arr[i][j]; }
you have validation mis-ordered in shortestpath
function:
if (arr[i][j] == 0 || == mr || j == mc || < 0 || j < 0 || p.searchcell(current)) {
you access array out of bounds before checking whether bounds valid. undefined behaviour, , different systems can behave differently in presence of undefined behaviour (so crash , appearing work both valid responses invoking undefined behaviour).
rewrite condition check indices ok before accessing array.
if (i == mr || j == mc || < 0 || j < 0 || arr[i][j] == 0 || p.searchcell(current)) {
this may not problem; 1 problem.
when take code verbatim question , compile it, compilation warnings (errors since use -werror
convert warnings errors).
$ 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 used uninitialized in function [-werror=maybe-uninitialized] if(p_array[i].length() < minsize && p_array[i].end() == target) { ^ 16.cpp:29:15: error: ‘minindex’ may used uninitialized in function [-werror=maybe-uninitialized] size = pp.size; ^ 16.cpp:134:9: note: ‘minindex’ declared here int minindex, minsize, i; ^ cc1plus: warnings being treated errors $
those serious problems , may account code going haywire. certainly, need fix before worth doing else.
i note have:
class path { int row[maxrow*maxrow]; int col[maxcol*maxcol];
at moment, maxrow == maxcol
there same number of elements in row
, col
, if numbers different, might not able store members of path in 1 or other of 2 arrays. not good. arrays should same size unconditionally. also, in main()
, define:
int arr[10][10], …
which should, of course, be:
int arr[maxrow][maxcol], …
with minimal fixups, setting minindex = -1
, minsize = -1
in shortestpath()
, , adding return *this;
assignment operator, , adding diagnostic printing @ end of shortestpath()
:
path shortestpath(int arr[][maxcol], int mr, int mc, cell current, cell target, path p) { int = current.row; int j = current.col; path p_array[4]; cerr << "-->> shortestpath(" << << "," << j << ")\n"; //if(arr[i][j] == 0 || == mr || j == mc || < 0 || j < 0 if (i == mr || j == mc || < 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 4 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; < 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; < 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]; } }
and running it, part of output is:
-->> shortestpath(2,0) <<-- shortestpath() - 1 3: minindex: -1 <<-- shortestpath() - 3
oops: accessing , returning p_array[-1]
not idea. have subscripts out of control — small wonder things go haywire. need meditate on code not setting minindex
value other -1
, it.
Comments
Post a Comment