0

Ok I am creating a distance matrix of cities. Seeing as the distance would be the same either way you travel between cities I am storing the data in vector and using a special indexing system in order to access it so that I dont have to store the data twice. The issue is when I go to output the data the program segfaults which makes no sense.

Below is the part of the code that prints out from the distance matrix. The matrix size is 630. and the last index output to cout is 629 which should mean I am fine. Somehow it isn't. Any clues?

output << "DISTANCE table:\n";
output << "size = : " << distance_mat.size() << endl;
int ibase,iindex;
int k = 0;
for(int i = 0; i < distance_mat.size(); i++){
    for(int j = 0; j <= i; j++){
        ibase = i*(i+1)/2;
        iindex = ibase + j;
        output << setw(3) << right << i << " " << cities[i].Name << " to " << cities[j].Name << ": ";
        cout << iindex << " " << distance_mat.size() << endl;
        output << fixed << setprecision(2) << distance_mat[iindex] << " miles\n";
    }
}
cout << "ummmmmmm?" << endl;

output.close();

Ummmmm is never output and the last value printed doesnt even match what distance_mat[629] would be.

A.O.
  • 3,733
  • 6
  • 30
  • 49
Robert Ryder
  • 39
  • 1
  • 8
  • 1
    Please, use debugger! – Ivan Aksamentov - Drop Mar 20 '14 at 21:32
  • I know this is going to seem like a really really dumb question but mind pointing me to a way to do that. Ive pretty much just learned c++ in a classroom setting and we only code in vim and compile in the command line so debuggers are kind of foreign to me. I have codelite which i think has a debugger. Ill see what it can do I guess. – Robert Ryder Mar 20 '14 at 21:36
  • You say it makes no sense. But my quick reading of that loop clearly places iindex out of bounds of the distance_mat container (I'm assuming it is a sequence container and not a map of some type). – PaulMcKenzie Mar 20 '14 at 21:38
  • @RobertRyder - If you are not an emacs user, the fewest key-strokes to launch a GDB, that works on Ubuntu 12.04 (but maybe needs ncurses installed), and will debug a program named "dumy54" is "gdb -tui dumy54" Inside gdb, type "b main", then, "run " appended with any number of command line parameters your app needs. If you have not yet found your error, and expect the seg-fault, press "c" for continue. When the seg-fault occurs, give command "up" to climb the call stack, or "bt" (back trace) to view how you got here. – 2785528 Mar 20 '14 at 21:52
  • @RobertRyder you can use `gdb` as Douglas noted, or just pick up any [IDE](http://stackoverflow.com/questions/24109/c-ide-for-linux). It will greatly improve you understanding of what's going on. – Ivan Aksamentov - Drop Mar 20 '14 at 22:10

1 Answers1

0

Supposingly distance_mat.size() = 630 then see the last iteration:

iindex = ibase + j = 629 * 630 / 2 = 198135

and then you try to access for j=0: distance_mat[iindex=198135] ... which you do not have... Something feels wrong with the algorithm.

Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167