I m programming C for an assingment in VC++ 2008. I simulate adjList for graph implementation. i can readly add edge between two vertex and print the graph.
and i want to remove edge between two vertex and print the graph again. whatever i do,i cant print the graph after deleting the edge. i get 0xfeefee 🙁 what is this? and how can i resolve this program.
my delete function and print the graph function are illustrated below.
void deleteEdge(Graph G, Vertex V, Vertex W)
{
Edge list,prev,temp;
list=V->list;
prev=NULL;
//
while(list!=NULL && list->to->value!=W->value){
prev=list;
list=list->next;
}
// have found the element.
if(list!=NULL){
temp=list;
// if first element of list is deleted.
if(prev==NULL)
list=list->next;
else
prev->next=list->next;
// reallocate.
free(temp);
}
}
void GRAPHprint(Graph G)
{
Vertex tmp;
Edge list;
for(tmp = G->head;tmp!=NULL;tmp=tmp->next)
{
fprintf(stdout,"V:%d\t",tmp->value);
list=tmp->list;
while(list!=NULL)
{
fprintf(stdout,"%d\t",list->to->value);
list=list->next;
}
fprintf(stdout, "\n");
}
system("pause");
}
In your code, if you match and hence try to remove the first edge of the list then you will end up dereferencing a pointer which you have freed.
list=list->nextshould beV->list=list->next, otherwise you are actually only updating your local list (list) rather than the input’s list (V->list).The
0xfeeefeeeindicates that you are reading deleted memory on the heap, i.e. that you are dereferencing a pointer on which you have earlier called free. This only happens in debug mode, it’s intend to help you catch this kind of problem! See this wikipedia entry for more information on this magic number (and others).