开发者

can i define an iterator inside a while loop?

My code has no compilation error. It seems to have a runtime error. In th开发者_运维技巧e following piece of code, when the while loop is executed for the first time everything works fine. But on the second iteration, everything stops at "list::iterator k = (temp2->store).begin();". When I comment out the "list::iterator k ..." part everything works fine. The variables temp2 and temp2->store are not NULL or empty. The debugger gave the following message "Unhandled exception at 0x009bc0cf in project euler.exe: 0xC0000005: Access violation reading location 0xddddddf5".

 struct edge
{  int end1, end2;    
   ptr_to_edge *ptr_end1, *ptr_end2;
   edge *next, *l_chd, *r_chd; 
   edge(): next(NULL), l_chd(NULL), r_chd(NULL),  ptr_end1(NULL), ptr_end2(NULL)
  { }
};


   struct ptr_to_edge {
        int flag, vertex;    
        list<edge*> store;
        ptr_to_edge(): flag(1)
        { }
     };
 void parallel_reduction_step(ptr_to_edge *front, vector<edge> &a) {

  list<edge*>::iterator next = (front->store).begin();
  next++;
  list<edge*>::iterator current = (front->store).begin();
  list<edge*>::iterator last = (front->store).end();
  --last;

  while(current!= last) {

      if(((*current)->end1 == (*next)->end1)&&((*current)->end2==(*next)->end2)) {

             edge temp1;
             temp1.end1 = (*current)->end1; temp1.end2 = (*current)->end2;
             a.push_back(temp1);

             ptr_to_edge *temp2;
             if ((*current)->end1==front->vertex) 
                 temp2 = (*current)->ptr_end2;
             else 
                 temp2 = (*current)->ptr_end1; 

             list<edge*>::iterator k = (temp2->store).begin();

              current = (front->store).erase(j);
             *current = &(a.back());
             }
      else current++;
      next++;
      }
  };


Thanks for cleaning up the code. It looks like you are pushing elements on vector a and then putting pointers to its elements in your other structures. The problem with this is that resizes of "a" (which can occur as you add to it) will cause pointers (or iterators) to its elements to become invalid, and this can occur as you push elements on it. I don't know if this is the problem you are having with the code, but it can cause problems. You can change "a" to deque which won't invalidate pointers to its elements as you add to the end, and it should fix that part.


Some more details on the error message you are getting when your program aborts might be helpful, but I will make a wild guess anyway. If your temp2 and temp2->store values are not NULL, then perhaps temp2->store is an empty container? That is, temp2->store.begin() == temp2->store.end(). You could try checking for that case before you attempt to dereference k. For example, you could change your while loop to

while(reached == 0 && k != temp2->store.end()) {
    .
    .
    .
}

If temp2->store is empty, then the begin iterator is equal to the end iterator, and dereferencing the end iterator is bad. So you should have a check to prevent it even if that ends up not being the problem.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜