Can't figure out this seg fault

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 56
Reputation: jasweb2002 is an unknown quantity at this point 
Solved Threads: 2
jasweb2002 jasweb2002 is offline Offline
Junior Poster in Training

Can't figure out this seg fault

 
0
  #1
Dec 5th, 2004
I am writing a program that will build a graph and then find the lowest costs between any given vertices. I have the program building the graph just fine, but now I just started to write the part that will find lowest costs and I get a seg fault error. After using cout's I have found that the entire program will run. But then right at the end when it should end, it suddenly hits a seg fault. Right now I am thinking it has something to do with the initialize function since this problem occured when that function was written. Any ideas? Thanks.

  1. // Main program
  2. #include <iostream>
  3. #include "graph.cc"
  4. using namespace std;
  5.  
  6. main ()
  7. {
  8.  
  9. graph flightfares;
  10. flightfares.build_graph();
  11. flightfares.print_graph();
  12. flightfares.find_shortest_dist();
  13. // My program will get right here then hit seg fault
  14. }
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. // Class and implementation program
  22. // graph.cc
  23. #include <iostream>
  24. #include <vector>
  25. #include <string>
  26. #include <float.h>
  27. using namespace std;
  28.  
  29.  
  30.  
  31.  
  32. struct costpair
  33. {
  34. int vnum;
  35. float fare;
  36. costpair(int v = 0, float f = 0):vnum(v), fare(f){};
  37. };
  38.  
  39.  
  40.  
  41.  
  42.  
  43. struct node
  44. {
  45. costpair info;
  46. node * next;
  47. };
  48.  
  49.  
  50.  
  51.  
  52.  
  53. class graph
  54. {
  55. public:
  56. graph();
  57. ~graph();
  58. void build_graph();
  59. void print_graph();
  60. void find_shortest_dist();
  61.  
  62.  
  63.  
  64. private:
  65. // Attributes
  66. vector<string> vnames;
  67. vector<node*> adjlist;
  68. double dist[];
  69. int pred[];
  70. bool undecided[];
  71.  
  72. // Member functions
  73. int index(string name);
  74. void insert_edge(string name1, string name2, float fare);
  75. int add_vertice(string name);
  76. void add_edge(int index1, costpair temp);
  77. node * createnode(costpair temp);
  78. void initialize();
  79. };
  80.  
  81. // Constructor
  82. graph::graph()
  83. {
  84.  
  85. }
  86.  
  87.  
  88.  
  89.  
  90. // Destructor
  91. graph::~graph()
  92. {
  93. for (int i = 0; i < vnames.size(); i++) // This loop will run and delete all lists
  94. {
  95. node * p = adjlist.at(i);
  96. node * temp;
  97. while (p!=NULL)
  98. {
  99. temp = p->next; // Saves pointer to rest of list
  100. delete p;
  101. p = temp;
  102. }
  103. }
  104. }
  105.  
  106.  
  107.  
  108. void graph::build_graph()
  109. {
  110. string name1, name2;
  111. int index1, index2;
  112. float fare;
  113.  
  114. cin >> name1 >> name2 >> fare; //Priming Read
  115. while(name1 != "zzz") // Runs till end of file
  116. {
  117. insert_edge (name1, name2, fare);
  118.  
  119. // This if checks for EOF and reads next line if present
  120. if(!(cin >> name1 >> name2 >> fare))
  121. {
  122. break;
  123. }
  124. }
  125. }
  126.  
  127.  
  128.  
  129.  
  130. // Returns the index of given name
  131. int graph::index(string name)
  132. {
  133. for (int i = 0; i < vnames.size(); i++)
  134. {
  135. if (vnames.at(i) == name) return i;
  136. }
  137.  
  138. return (-1); // Name was not found
  139. }
  140.  
  141.  
  142.  
  143. // Function to insert new edge
  144. // Called once two names and a fare have been read
  145. void graph::insert_edge(string name1, string name2,
  146. float fare)
  147. {
  148. int index1 = index(name1);
  149. // Next if checks if first name needs to be added
  150. if (index1 == -1) index1 = add_vertice(name1);
  151. int index2 = index(name2);
  152. // Next if checks if second name needs to be added
  153. if (index2 == -1) index2 = add_vertice(name2);
  154. // At this point, index1 and index2 contain the indices of name1 and name2
  155.  
  156. // This adds the edge
  157. add_edge(index1, costpair(index2, fare));
  158. }
  159.  
  160.  
  161.  
  162.  
  163. // This functin addes a name to the name vector
  164. // and pushes the adjacency vector back.
  165. // Then the new index of the name is returned
  166. int graph::add_vertice(string name)
  167. {
  168. vnames.push_back(name);
  169. adjlist.push_back(NULL);
  170. return (vnames.size()-1);
  171. }
  172.  
  173.  
  174.  
  175.  
  176. // Here a node is added to the adjacency vector
  177. void graph::add_edge(int index1, costpair temp)
  178. {
  179. node * p = createnode(temp); // Creates node
  180.  
  181. // Adds node to appropriate list
  182. p->next = adjlist.at(index1);
  183. adjlist.at(index1) = p;
  184.  
  185. }
  186.  
  187.  
  188.  
  189. // This function creates a node and adds info to node
  190. node * graph::createnode(costpair temp)
  191. {
  192. node * p = new node;
  193. p->info = temp;
  194. p->next = NULL;
  195. return p;
  196. }
  197.  
  198.  
  199.  
  200. // This fuction prints all vertices along with edges
  201. void graph::print_graph()
  202. {
  203. for (int i = 0; i < vnames.size(); i++) // Runs for all vertices
  204. {
  205. cout << "The vertex is " << vnames.at(i) << endl;
  206. node * p = adjlist.at(i); // Pointer to traverse list
  207. if (p == NULL) // Checks if list is empty or not
  208. {
  209. cout << "This vertex has no neighbors" << endl;
  210. }
  211. else
  212. {
  213. cout << "The neighbors of this node are " << endl;
  214. while (p!=NULL)
  215. {
  216. cout << vnames.at(p->info.vnum) << "(" << p->info.fare << ")" << endl;
  217. p = p->next;
  218. }
  219. }
  220. }
  221. }
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228. void graph::find_shortest_dist()
  229. {
  230. initialize(); // Will initialize the several arrays needed
  231.  
  232. }
  233.  
  234.  
  235. void graph::initialize()
  236. {
  237. dist[vnames.size()];
  238. pred[vnames.size()];
  239. undecided[vnames.size()];
  240.  
  241.  
  242. for (int i = 0; i < vnames.size(); i++)
  243. {
  244. dist[i] = DBL_MAX;
  245. pred[i] = -1;
  246. undecided[i]= false;
  247. }
  248. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Can't figure out this seg fault

 
0
  #2
Dec 6th, 2004
  1. dist[vnames.size()];
  2. pred[vnames.size()];
  3. undecided[vnames.size()];
???
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 56
Reputation: jasweb2002 is an unknown quantity at this point 
Solved Threads: 2
jasweb2002 jasweb2002 is offline Offline
Junior Poster in Training

Re: Can't figure out this seg fault

 
0
  #3
Dec 6th, 2004
I had a feeling my grasp of declaring the arrays was the problem. Could anyone tell me why the seg fault occured though after the entire program ran instead of when it hit that bad code?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Can't figure out this seg fault

 
0
  #4
Dec 6th, 2004
main ()
{
 
  graph flightfares;
  flightfares.build_graph();
  flightfares.print_graph();
  flightfares.find_shortest_dist();
  // My program will get right here then hit seg fault
}
void graph::find_shortest_dist()
{
  initialize();  // Will initialize the several arrays needed

}
void graph::initialize()
{
  dist[vnames.size()];
  pred[vnames.size()];
  undecided[vnames.size()];
  undecided[vnames.size()];

  for (int i = 0; i < vnames.size(); i++)
    {
      dist[i] =  DBL_MAX;
My guess would be right about here -- right where you say it does.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC