开发者

How can i get rid of 0xFEEFEE in VC

开发者 https://www.devze.com 2022-12-22 18:15 出处:网络
I m programming Cfor anassingment in VC++ 2008. Isimulate adjList forgraph implementation. i can re开发者_运维百科adly add edge between two vertex and print the graph.

I m programming C for an assingment in VC++ 2008. I simulate adjList for graph implementation. i can re开发者_运维百科adly 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->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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号