I'm writing a graph and I've run into a strange problem. In the actual code, whenever I set a pointer to 'NULL', it always sets it to 'OxFFFFFFF'. However, in the client code, it does not recognize this value as NULL which always leads to a segmentation error as it tries to access members of a NULL class. This is the code in question that should add edges to vertex: Here are some of the object definitions:
struct EdgeNode // Structure representing an edge
{
VertexNode* destination; // Pointer to destination vertex
int weight; // Edge weight
EdgeNode* nextPtr; // Pointer to next edge
};
struct VertexNode // Structure representing a vertex
{
string vname; // Name of vertex
bool mark; // Marked flag
EdgeNode* edgePtr; // Pointer to list of outgoing edges
VertexNode* nextVertex; // Pointer to next vertex in vertices list
};
And here is the code where the problem is coming from:
void Graph::AddEdge(string s, string d, int w){
try{ //try block to make sure there's memory
EdgeNode* nextedge = new EdgeNode;
delete nextedge;
}
catch(std::bad_alloc){
throw GraphFull();
}
VertexNode* cur = vertices; //vertices is the location of the base vertice in the code
EdgeNode* curedge; //current edge
VertexNode* dest = vertices; //start the destination pointer from the base address, and try to find the destination
EdgeNode* nextedge = new EdgeNode;
nextedge->weight = w;
nextedge->nextPtr = NULL;
bool finaldest = false;
while (finaldest == false && dest!=NULL){ /*here we loop till we get to the destination. vname is the string value of each vertex*/
if (dest->vname == d){vertex found
finaldest = true;
}
else{
if (dest->nextVertex!=NULL){
dest = dest->nextVertex;
if (dest->vname == d){
finaldest = true;
}
}
}
}
nextedge->destination = dest; //here we set the destination in our edge struct to the location of the vertex we just found
//Now we try to find the start point of the edge
while (cur->vname !=s){
cur = cur->nextVertex;
}
if (cur->vname == s){ //here we find the start point and try to insert the edge
if(cur->edgePtr == NULL){
cur->edgePtr = nextedge; //if the edgeptr is empty, set it to the first edge
}
else{ //else add the edge and make the original edge be the nextedge that the current edge is pointing to (yes this is strange but my professor wanted it that way)
EdgeNode* edger = cur->edgePtr;
nextedge->nextPtr = edger;
cur->edgePtr = nextedge;
}
}
}
And here is the client function that causes the error:
// Print -- write graph to stdout. DO NOT MODIFY THIS FUNCTION!!!
void Print()
{
EdgeNode* eptr;
VertexNode* vptr = vertices;
const int FIELDWIDTH = 6;
string STARS = "**********";
STARS = STARS + STARS + STARS;
cout << endl << STARS << endl;
cout << setw(FIELDWIDTH) << "Vertex" << " : " << "Adjacent Vertices" << endl;
cout << "------------------------------" << endl;
while(vptr != NULL)
{
cout << setw(FIELDWIDTH) << vptr->vname << " : ";
eptr = vptr->edgePtr;
while (eptr != NULL)
{
cout << eptr->destination->vname << eptr->weight << " ";
cout<<"edge pointer is '"<<eptr<<"'"<<endl;
eptr = eptr->nextPtr; //Here is where the code crashes, because when it gets to NULL, it reads '0xFFFFFF' as a pointer when it is actually null, then moves into it, and since it as no members, it crashes.
}
cout << endl;
vptr = vptr->nextVertex;
}
cout << STARS << endl << endl;
} // Graph::Print()
};
I'm not sure where the problem is but I would appreciate any hints.
User contributions licensed under CC BY-SA 3.0