Josh Black
Josh Black

Reputation: 999

Segmentation Fault on Ubuntu Machine but not on Mac

Was working on an assignment on a Mac machine and the code compiles and I get my expected output. As soon as I go to test it on the Ubuntu machines that the programs are run on for grading I get a Segmentation fault (core dumped) error message. I am really stumped and am looking for some tips for debugging why this occurs on the ubuntu machine (version 12.04) and not the mac machine (mavericks OS).

Edit: Running it through gdb I get the segmentation fault at

in getPath: line 28, if (path[position] == 0 && position == 0) {

My code is as follows:

#include <iostream>

using namespace std;

int minDistance(int distance[], bool shortestPath[]) {

    int min = 1000000000;
    int indexOfMin;

    for (int i = 0; i < 6; i++) {
        if (shortestPath[i] == false && distance[i] <= min) {
            min = distance[i]; 
            indexOfMin = i;
        }
    }

    return indexOfMin;
}

void getPath(int path[], int position) {
    cout << position + 1 << " <- ";

    // base case, we hit the end of the path
    if (path[position] == 0 && position == 0) {
        cout << path[position] + 1 << endl;
    }
    else if (path[position] == 0)
        cout << path[position] + 1 << endl;
    else {
        getPath(path, path[position]);
    }
}

void dijkstraAlgorithm(int weightedGraph[6][6], int sourceVertex) {
    int distance[6];     
    bool shortestPath[6]; 
    int path[6]; 

    for (int i = 0; i < 6; i++) {
        distance[i] = 1000000000; 
        shortestPath[i] = false;
    }

    distance[sourceVertex] = 0;

    for (int j = 0; j < 5; j++) {
        int min = minDistance(distance, shortestPath);
        shortestPath[min] = true;
        for (int k = 0; k < 6; k++) {
            if (!shortestPath[k] && weightedGraph[min][k] && distance[min] != 1000000000 && distance[min] + weightedGraph[min][k] < distance[k]) {
                distance[k] = weightedGraph[min][k] + distance[min];
                path[k] = min;
            }
        }
    }

    cout << "Distance       Path" << endl;
    for (int i = 0; i < 6; i++) {
        // dist[i] == 0 a formatting fix for first distance
        if (distance[i] == 0) {
            cout << distance[i] << ":            ";
            getPath(path, i);
        }
        else {
            cout << distance[i] << ":           ";
            getPath(path, i);
        }
    }
}

int main() {
    int weightedGraph[6][6] = {
        {0, 10, 0, 30, 100, 0},
        {0, 0, 50, 0, 0, 0},
        {0, 0, 0, 0, 10, 5},
        {0, 0, 20, 0, 0, 15},
        {0, 0, 0, 60, 0, 0},
        {0, 0, 0, 0, 0, 0}
    };

    dijkstraAlgorithm(weightedGraph, 0);

    return 0;
}

Note: I'm required to use the g++ compiler (version 4.6.4) on the Ubuntu environment

Upvotes: 2

Views: 3680

Answers (2)

Drew MacInnis
Drew MacInnis

Reputation: 8587

Running your sample code under valgrind shows these uninitialized read errors:

==25442== Conditional jump or move depends on uninitialised value(s)
==25442==    at 0x4008B2: getPath(int*, int) (crash.cpp:24)
==25442==    by 0x400B19: dijkstraAlgorithm(int (*) [6], int) (crash.cpp:62)
==25442==    by 0x400B9C: main (crash.cpp:81)

and:

==25442== Conditional jump or move depends on uninitialised value(s)
==25442==    at 0x4008F8: getPath(int*, int) (crash.cpp:27)
==25442==    by 0x400B19: dijkstraAlgorithm(int (*) [6], int) (crash.cpp:62)
==25442==    by 0x400B9C: main (crash.cpp:81)

among others.

Don't assume these arrays will be automatically initialized with zero (0):

int distance[6];
bool shortestPath[6];
int path[6];

Initialize these arrays and you'll eliminate some of the problems (at least).

Refer to Initialization of a normal array with one default value.

Updated: Why it works on the Mac is likely just happy coincidence, as mentioned in the answer to Turn off automatic initialization of variables in Xcode

Upvotes: 2

Michael Anderson
Michael Anderson

Reputation: 73520

If you're doing something bad (invoking undefined behaviour), then an application can work on one OS and not on another. For C and C++ the list of things that can induce undefined behaviour is large... But in your case its probably an out-of-bounds memory access.

To debug it on linux I would run the application through valgrind, which will tell you when you make the first invalid access, (rather than the one that crashes your code - which may not be the first.).

If you're stuck on OS X I'd first run the code through the static analyser that is part of XCode. It will sometimes warn you about stupid accesses. Then after that I'd look at using the malloc debugging tools to help you narrow down the invalid access. "man malloc" will give you a list of the available environment variables.

Upvotes: 1

Related Questions