943,975 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 414
  • C++ RSS
Dec 2nd, 2008
1

C++ Segement Fault on Unix

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 46
Solved Threads: 0
Newbie Poster
yingfo is offline Offline
13 posts
since Sep 2008
Dec 2nd, 2008
0

Re: C++ Segement Fault on Unix

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.
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 2nd, 2008
0

Re: C++ Segement Fault on Unix

Thank you that worked.
I really appreciate your help!
Reputation Points: 46
Solved Threads: 0
Newbie Poster
yingfo is offline Offline
13 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Comparisons
Next Thread in C++ Forum Timeline: Finding Positon of characters





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC