Seg fault and bus error

Oct 27, 2015 at 1:10pm
Hi,
Can anyone give examples on what cause segmentation fault and bus error? How do you know which part of the code is problematic and how should I resolve the error? I tried printing stuff throughout the code to see where the error is but sometimes nothing comes out at all.

For the code below, I got a bus error. What is causing it? Initially, I had a segmentation fault.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
using namespace std;

int maxu = -1;

void  utility(int x, int y, string mat[12][12],int val,int steps){
    if (steps == 0){
        maxu = max(maxu, val);
        return;
        }
    else if (mat[x][y]=="d" || mat[x][y] == "v"){
        return;
    } else {
    string ori = mat[x][y];
    int curr  = atoi(ori.c_str());
    mat[x][y] = "v";
    utility(x-1,y,mat,val+curr,steps-1); //north
    utility(x,y+1,mat,val+curr,steps-1); //east
    utility(x+1,y,mat,val+curr,steps-1); //south
    utility(x,y-1,mat,val+curr,steps-1); //south
    mat[x][y] = ori;
    }
}
int main(){
    int N;
    cin >> N;
    string mat[12][12] = {"d"};
    int row = 0;
    int col = 0;
    for (int i=1;i<=N;i++){
        for (int j=1;i<=N;j++){
                string x;
                cin >> x;
                if (x == "*"){row = i; col = j; mat[i][j]="v";}
                mat[i][j] = x;
            }
        }
    utility(row+1,col,mat,0,10);
    utility(row-1,col,mat,0,10);
    utility(row,col-1,mat,0,10);
    utility(row,col+1,mat,0,10);
    cout << maxu << endl;
    return 0;
}
Oct 27, 2015 at 1:27pm
Oct 27, 2015 at 1:32pm
33
34
    for (int i=1;i<=N;i++){
        for (int j=1;i<=N;j++){


This will cause you to go past the end of the array. Arrays start from zero, the normal form is this:

for (int i=0;i < N; i++){

There are problems with the way you get input. You don't prompt for input, then use an array with a size that is unknown to the user, and have no check to see if the input value is valid.

Having a std::cin inside a nested for loop without a prompt is confusing and long winded - it's 144 inputs. You need to come up with a better way.

Can you explain the recursion? You have 4 calls to the function in main, then the function itself has 4 recursive calls - how does it ever get to the last 3? It seems to be really obscure.
Oct 27, 2015 at 2:13pm
The input, N, is guaranteed to be from 4-10. So will I go past the end of my array?

The qn involves a n*n square and each square has some value (might be double digit). I am supposed to start at * and find a path of length 10 that sums to the greatest value and return the value. However, I can't visit the same square again.

So while I put the input data in the matrix, I marked all the other squares as "d" so I would have a "border" that my recursion cannot go pass so it will be within the n*n square.

And after I visited a square, I will mark it with a "v" then try going north, south, east and west.

The base case is when I finish 10 steps or encountered a blocked path ("d" or "v")
Topic archived. No new replies allowed.