C++ Segement Fault on Unix

Thread Solved
Reply

Join Date: Sep 2008
Posts: 9
Reputation: yingfo is on a distinguished road 
Solved Threads: 0
yingfo yingfo is offline Offline
Newbie Poster

C++ Segement Fault on Unix

 
1
  #1
Dec 2nd, 2008
Hi I was wondering if anyone could help
I'm receiving a segmentation fault whenever I go to run
my main.cpp on my program. I've never encountered this before
and was wondering if anyone knew how it can be fixed.

The program consist of equiv.h, equiv.cpp, graph.h, graph.cpp, and main.cpp

here are the files

equiv.h
  1. const int maxVertices = 50;
  2.  
  3. /*******************************************
  4.  * Equiv *
  5.  *******************************************
  6.  * Equiv is a structure type that holds a *
  7.  * pointer to an array of integers and an *
  8.  * integer of the size of that array. *
  9.  *******************************************/
  10. struct Equiv
  11. {
  12. int* arrayIntegers;
  13. int arraySize;
  14. };
  15.  
  16. bool together(Equiv& e, int x, int y);
  17. void combine(Equiv& e, int x, int y);


equiv.cpp

  1. #include <iostream>
  2. #include <fstream>
  3. #include "equiv.h"
  4.  
  5. /**********************************************
  6.  * together *
  7.  **********************************************
  8.  * together returns true if x and y are in the*
  9.  * same set as object e. *
  10.  **********************************************/
  11. bool together(Equiv& e, int x, int y)
  12. {
  13. bool sameX = false;
  14. bool sameY = false;
  15.  
  16. for(int i = 0; i < e.arraySize; i++)
  17. {
  18. if(e.arrayIntegers[i] == x)
  19. sameX = true;
  20. if(e.arrayIntegers[i] == y)
  21. sameY = true;
  22. if(sameX && sameY)
  23. break;
  24. }
  25. return (sameX && sameY);
  26. }
  27.  
  28. /**********************************************
  29.  * combine *
  30.  **********************************************
  31.  * combine puts x and y together by combining *
  32.  * the two into the same set. *
  33.  **********************************************/
  34. void combine(Equiv& e, int x, int y)
  35. {
  36. if(together(e, x, y))
  37. return;
  38.  
  39. bool switchX = false;
  40. bool switchY = false;
  41.  
  42. if(e.arraySize > 0)
  43. {
  44. for(int i = 0; i < e.arraySize; i++)
  45. {
  46. if(x == e.arrayIntegers[i])
  47. switchX = true;
  48. if(y == e.arrayIntegers[i])
  49. switchY = true;
  50. if(switchX && switchY)
  51. break;
  52. }
  53. }
  54. else
  55. {
  56. e.arraySize = 0;
  57. e.arrayIntegers = new int[maxVertices];
  58. }
  59.  
  60. if(!switchX && !switchY)
  61. {
  62. if(x < y)
  63. {
  64. e.arrayIntegers[e.arraySize] = x;
  65. e.arrayIntegers[e.arraySize+1] = y;
  66. }
  67. else
  68. {
  69. e.arrayIntegers[e.arraySize] = y;
  70. e.arrayIntegers[e.arraySize+1] = x;
  71. }
  72. switchX = switchY = true;
  73. e.arraySize += 2;
  74. }
  75.  
  76. if(!switchX)
  77. e.arrayIntegers[e.arraySize++] = x;
  78. if(!switchY)
  79. e.arrayIntegers[e.arraySize++] = y;
  80. }

graph.h

  1.  
  2. #include <iostream>
  3. const int maxEdges = 100;
  4.  
  5.  
  6.  
  7. /**********************************************
  8.  * Edge *
  9.  **********************************************
  10.  * Edge is a structure that holds two vertices*
  11.  * and the weight. *
  12.  **********************************************/
  13. struct Edge
  14. {
  15. int vertOne;
  16. int vertTwo;
  17. int weight;
  18. };
  19.  
  20.  
  21.  
  22. /**********************************************
  23.  * Graph *
  24.  **********************************************
  25.  * Graph is a structure that holds the number *
  26.  * of vertices, the number of edges, and a *
  27.  * pointer to the array of edges. *
  28.  **********************************************/
  29.  
  30. struct Graph
  31. {
  32. int numVert;
  33. int numEdges;
  34. Edge * arrayEdges;
  35. ~Graph()
  36. {
  37. delete [] arrayEdges;
  38. }
  39. };
  40.  
  41.  
  42.  
  43.  
  44. void readGraph(Graph& g);
  45. void printGraph(Graph& g);
  46. void kruskalAlg(Graph& g);
  47. int totalWeight(Graph& g);

graph.cpp

  1. #include <iostream>
  2. #include <fstream>
  3. #include "graph.h"
  4. #include "equiv.h"
  5.  
  6. using namespace std;
  7.  
  8.  
  9.  
  10. /**********************************************
  11.  * readGraph *
  12.  **********************************************
  13.  * readGraph reads in information about graph *
  14.  * g from standard input then sets the edges *
  15.  * to the maximum number of edges avaliable. *
  16.  **********************************************/
  17. void readGraph(Graph& g)
  18. {
  19. int input;
  20. cin >> g.numVert;
  21. bool finished;
  22. finished = false;
  23. if(g.numEdges > 0)
  24. delete [] g.arrayEdges;
  25. g.arrayEdges = new Edge[maxEdges];
  26. int k = 0;
  27. while(!finished)
  28. {
  29. cin >> input;
  30. if(input==0) break;
  31. g.arrayEdges[k].vertOne = input;
  32. cin >> g.arrayEdges[k].vertTwo;
  33. cin >> g.arrayEdges[k].weight;
  34. k++;
  35. }
  36. g.numEdges = k;
  37. }
  38.  
  39.  
  40. /************************************************
  41.  * printGraph *
  42.  ************************************************
  43.  * printGraph prints out each vertice and weight*
  44.  * of graph g from the standard input. *
  45.  **********************************************/
  46.  
  47. void printGraph(Graph& g)
  48. {
  49. cout << "Vertices Weight" << endl;
  50. for(int k=0; k < g.numEdges; k++)
  51. cout << g.arrayEdges[k].vertOne << " " << g.arrayEdges[k].vertTwo<< "\t " << g.arrayEdges[k].weight << endl;
  52. }
  53.  
  54.  
  55. /**********************************************
  56.  * kruskalAlg *
  57.  **********************************************
  58.  * kruskalAlg computes the minimal spanning *
  59.  * tree using Kruskal's algorithm, it takes *
  60.  * graph g as a parameter and yields it to *
  61.  * graph k. *
  62.  **********************************************/
  63.  
  64. void kruskalAlg(Graph& g)
  65. {
  66. Graph k;
  67. Equiv e;
  68. k.numVert = g.numVert;
  69. k.numEdges = 0;
  70. k.arrayEdges = new Edge[maxEdges];
  71. e.arraySize = 0;
  72. e.arrayIntegers = 0;
  73. int a = g.numEdges-1;
  74. while(a > 1)
  75. {
  76. for(int i = 0; i < a; i++)
  77. {
  78. if(g.arrayEdges[i].weight > g.arrayEdges[i+1].weight)
  79. {
  80. Edge edgeOne = g.arrayEdges[i+1];
  81. g.arrayEdges[i+1] = g.arrayEdges[i];
  82. g.arrayEdges[i] = edgeOne;
  83. }
  84. }
  85. a--;
  86. }
  87.  
  88. for(int i = 0; i < g.numEdges; i++)
  89. {
  90. if(!together(e, g.arrayEdges[i].vertOne, g.arrayEdges[i].vertTwo))
  91. {
  92. combine(e, g.arrayEdges[i].vertOne, g.arrayEdges[i].vertTwo);
  93. k.arrayEdges[k.numEdges++] = g.arrayEdges[i];
  94. }
  95. }
  96. g.numEdges = k.numEdges;
  97. delete [] g.arrayEdges;
  98. g.arrayEdges = new Edge[maxEdges];
  99. for(int i = 0; i < k.numEdges; i++)
  100. g.arrayEdges[i] = k.arrayEdges[i];
  101. }
  102.  
  103.  
  104. /**********************************************
  105.  * totalWeight *
  106.  **********************************************
  107.  * totalWeight computes the total weight of *
  108.  * graph g. *
  109.  **********************************************/
  110.  
  111. int totalWeight(Graph& g)
  112. {
  113. int total = 0;
  114. for(int k = 0; k < g.numEdges; k++)
  115. total += g.arrayEdges[k].weight;
  116. return total;
  117. }

main.cpp
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include "graph.h"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. Graph g;
  10. readGraph(g);
  11. cout << endl << "The input graph has " << g.numVert << " vertices, and its edges are as follows:" << endl << endl;
  12. printGraph(g);
  13. kruskalAlg(g);
  14. cout << endl << "A minimal spanning tree uses the following edges." << endl << endl;
  15. printGraph(g);
  16. cout << endl << "The total weight of the spanning tree is " << totalWeight(g) << endl << endl;
  17. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,152
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: C++ Segement Fault on Unix

 
0
  #2
Dec 2nd, 2008
My guess is that in main.cpp, the Graph object has not been initiaized, so when ReadGraph() is called and attempts to delete arrayEdges, CRASH/COREDUMP.

In main() initialize all Graph variables to 0 before calling ReadGraph() to see if that resolves your problem.
  1. int main()
  2. {
  3. Graph g;
  4. memset(&g, 0, sizeof(Graph));
  5. readGraph(g);
  6. <snip>
Last edited by Ancient Dragon; Dec 2nd, 2008 at 10:33 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 9
Reputation: yingfo is on a distinguished road 
Solved Threads: 0
yingfo yingfo is offline Offline
Newbie Poster

Re: C++ Segement Fault on Unix

 
0
  #3
Dec 2nd, 2008
Thank you that worked.
I really appreciate your help!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC