Segmentation fault (core dumped)

Okay well I'm not to advanced at C++, its only my 2 quarter in college taking Computer Science so please don't say anything that will go straight over my head haha. Well the code I have compiles just fine but when I got to run it I'm getting this seg fault error and I'm pretty sure it has something to do with the pointers in my code, if someone could just look at it and help me out id appropriate it. Thanks
So what this code is trying to do is read a .dat file that has 3 columns the first two are numbers that represent cities and the third is the cost of a cable that is connected between them. It will read the file and then find the cheapest cost that has all cities connected some way or form.
My Code:


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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <time.h>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

class Cable{ public: int city1, city2, cost; bool solution; };

int costcompare(const void *x, const void *y) {
  if ((**(Cable **)x).cost < (**(Cable**)y).cost) return -1;
  if ((**(Cable **)x).cost > (**(Cable**)y).cost) return 1;
  return 0;
}

class Solver {
  Cable **cables;
  int ncables;
  int ncities;
  int *group;
  Cable **argv;
  int sum;
  int dummy;
  string buffer;
  fstream *fin;

public:
  //constructor
  Solver() {
    this->ncables = ncables;
    this->ncities = ncities;
    this->sum = sum;
    this->dummy = dummy;
    this->buffer = buffer;
    this->cables = cables;
    this->group = group;
    this->fin = fin;
    this->argv = argv;
    }

  //reads the file from main
  void readFile(char **argv) {
    fin = new fstream(argv[1], ios::in);
    if (fin->fail()) {
      cerr << "Cannot open file " << argv[1] << "\n";
      exit(0);
    } 

    while (getline(*fin, buffer)) ncables++;
    fin->close();
    fin->clear();

    cables = new Cable*[ncables];

    sum = 0;
    fin->open(argv[1], ios::in);
    for (int i = 0; i < ncables; i++) {
      cables[i] = new Cable();
      *fin >> cables[i]->city1>> cables[i]->city2 >> cables[i]->cost;
      sum += cables[i]->cost;
    }
    fin->close();
    fin->clear();
  }

  // Finds the number of cities
  void  findNcity () {
    ncities = 0;
    for (int i=0; i < ncables; i++) {
      if (cables[i]->city1 > ncities) ncities = cables[i]->city1;
      if (cables[i]->city2 > ncities) ncities = cables[i]->city2;
      }
  }

  //gives the group their numbers
  void giveGroupnum() {
    group = new int[ncities + 1];
    for (int i=0; i <= ncities; i++) group[i] = i;
  }

  //the cycle
  bool isCycle(Cable *cable) {
    if (group[cable->city1] == group[cable->city2]) return false;
    dummy = group[cable->city2];
    for (int i = 0; i <= ncities; i++)
      if (group[i] == dummy) group[i] = group[cable->city1];
    return true;
  }


  //Solves the problem
  void solve() {
    qsort(cables, ncables, sizeof(Cable*), costcompare);
    for (int i=0; i < ncables; i++) {
      if (isCycle(cables[i])) cables[i]->solution = true;
      else cables[i]->solution = false;
    }
    for (int i=0; i < ncables; i++){
      if (cables[i]->solution == true) 
	cout << i << ": " << cables[i]->city1 << " "
	     << cables[i]->city2 << " "
	     << cables[i]->cost << "\n"; }
  }

};

int main(int argc, char **argv)
{
  if (argc != 2) {
    cout << "\nUsage: " << argv[0] << " \n";
    exit (0);
  }

  Solver mysolver;

  mysolver.readFile (argv);
  mysolver.findNcity ();
  mysolver.giveGroupnum();
  mysolver.solve();
  return 0;
}
Last edited on
You should try using a debugger to find where the problem is. Also, in the future you should use [code][/code] tags on this forum.
Sooooo where would i get a debugger lol. what is a ? sorry I just made a account on here.
Last edited on
How are you compiling your code/what OS are you using?

And for the tags, you just put them around your code so this:
[code]int main() {
int i = 0;
return 0;
}[/code]
becomes
1
2
3
4
int main() {
    int i = 0;
    return 0;
}
Oh i see. Umm im using the terminal on Ubuntu (Linux operating system)
Then you probably have "gdb" to debug your code. You can try reading the man pages for gdb, but I'd recommend looking for a tutorial on the internet.
I was looking around and is something called ruby a debugger?
Ruby is a programming language, not a debugger. Does your system not have gdb?
yes i have gdb
Can anyone help me out with this code?
You does not init you class members, the core position is "Cable **cables = new Cable*[ncables];", because ncables not init. you construct function Solver() should init all members. like this :
1
2
3
4
5
6
7
8
9
10
  Solver() {
    this->ncables = 0;
    this->ncities = 0;
    this->sum = 0;
    this->dummy = 0;
    this->cables = NULL;
    this->group = NULL;
    this->fin = NULL;
    this->argv = NULL;
  }


I'm chinese , so bad english.
Last edited on
i just tried this and now it wont even compile.
你真是太菜鸟了!
Ya i know im a rookie i clearly stated it up top.
First, let me say that this class is a horrible mess and needs to be completely redesigned.

That said, if you declare a variable in a member function of a class that has the same name as a class variable, you hide the class variable. It looks like you think doing this is the way to access the class variable. It is not.

Take your:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//reads the file from main
  void readFile(char **argv) {
    fstream *fin = new fstream(argv[1], ios::in);
    if (fin->fail()) {
      cerr << "Cannot open file " << argv[1] << "\n";
      exit(0);
    } 

    while (getline(*fin, buffer)) ncables++;
    fin->close();
    fin->clear();

    Cable **cables = new Cable*[ncables];

    int sum = 0;
    fin->open(argv[1], ios::in);
    for (int i = 0; i < ncables; i++) {
      cables[i] = new Cable();
      *fin >> cables[i]->city1>> cables[i]->city2 >> cables[i]->cost;
      sum += cables[i]->cost;
    }
    fin->close();
    fin->clear();
  }



I suspect you should be manipulating the cables variable that is a part of the class, not a local variable with the same name. When the function returns, the local cables is discarded and you have a nice memory leak. Also, there's no reason to use new for your fstream here (especially as you don't release the memory you allocate.) This should probably look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//reads the file from main
  void readFile(char **argv) {
    ifstream fin(argv[1]);
    if (fin.fail()) {
      cerr << "Cannot open file " << argv[1] << "\n";
      exit(0);
    } 

    while (getline(fin, buffer)) ncables++;
    fin.close();
    fin.clear();

    cables = new Cable*[ncables];

    sum = 0;
    fin.open(argv[1]);
    for (int i = 0; i < ncables; i++) {
      cables[i] = new Cable();
      fin >> cables[i]->city1>> cables[i]->city2 >> cables[i]->cost;
      sum += cables[i]->cost;
    }
}


And of course you need to make similar changes in all of your member functions, but before you do that consider chucking the entire design, because it sucks. =)
Me being still a noob at this i got a bit lost in the vocab haha.
Topic archived. No new replies allowed.