tags:

views:

122

answers:

2

My code has no compilation error. It seems to have a runtime error. In the 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++;
      }
  };
+2  A: 

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.

ergosys
i have renamed the iterators. But iterator last(or m from the unedited post) is not the problem..
i will try it out and get back to you on it.. I have also included the struct definitions if that would be of any help..
+1 `deque` should be the de-facto container!
Matthieu M.
A: 

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.

A. Levy
temp2->store is not empty
Is it possible that it will ever be empty? In any use case of your code? I would still put that check in there even if it doesn't solve your problem. It will probably avoid another problem some time down the road.
A. Levy
added the k != temp2->store.end() part to the while condition . but the problem seems to persist.