c++ - Thread 1: EXC_BAD_ACCESS (code=1, adress=0x0) -
c++ - Thread 1: EXC_BAD_ACCESS (code=1, adress=0x0) -
i'm making dijikstra algorithm in c++ (xcode). i'm getting exc_bad_access code=1 @ moment:
(k=0; k<m; k++){ input>>i>>j>>v; graf[i][j] = v; }
whole code:
#include <iostream> #include <fstream> int min(int[]); int **graf; int *label; int *active; int i, j, k; int start, n, m, v, last; using namespace std; int min(int array[]) { int min, k, min_pos = -1; min = 32767; (k=0; k<m; k++) { if (array[k] < min && active[k] == 1){ min = array[k]; min_pos = k; } } homecoming min_pos; } int main(){ ifstream input ("input.txt"); input>>n>>m>>start>>last; if (!input){ cout << "file not found" << endl; homecoming 1; } graf = new int *[n]; (i=0; i<n; i++){ graf[i] = new int [n]; } (i=0; i<n; i++){ (j=0; j<n; j++){ graf[i][j]=0; } } label = new int [n]; active = new int [n]; (i=0; i<n; i++){ label[i] = 0; active[i] = 0; } (k=0; k<m; k++){ input>>i>>j>>v; graf[i][j] = v; } (i=0; i<m; i++){ label[i] = 32767; active[start] = 1; } label[start] = 0; = start; { (j=0; j<n; j++) if (graf[i][j] != 0 && label[j] > label[i] + graf[i][j]){ active[j] = 1; label[j] = label[i] + graf[i][j]; } active[i] = 0; i=min(label); } while (i != -1); cout << label[last] << endl; homecoming 0; }
i'm looking error days, can't find.
input.txt 5 9 0 4 0 0 5 2 4 0 0 0 0 7 0 6 5 0 0 9 1 15 2 7 9 0 0 12 4 0 1 0 0 8 0 6 15 12 8 0
i believe error in input file: read , j , utilize access arrays, without verifying these value within expected boundaries.
for instance, if improve questioned code error message, like:
for (k=0; k<m; k++){ input>>i>>j>>v; if (i>=n || j>=n) { cerr << "bad input k="<<k<<": "<<i<<","<<j<<","<<v<<endl; continue; } //assert(i<n && j<n); variant if don't care values graf[i][j] = v; }
you'll next results:
bad input k=3: 7,0,6 bad input k=4: 5,0,0 bad input k=5: 9,1,15 bad input k=6: 2,7,9
c++ xcode algorithm
Comments
Post a Comment