tags:

views:

125

answers:

2

Hi guys 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");
    }
+3  A: 

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->next should be V->list=list->next, otherwise you are actually only updating your local list (list) rather than the input's list (V->list).

The 0xfeeefeee indicates 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).

Tom
+1  A: 

When the edge you delete is the first element in V->list, this V->list pointer will keep pointing to the freed element.

The prev==NULL case is trying to handle this problem, but you only advance the temporary local list pointer to the next element. You should change V->list instead to adjust the main data structure.

sth