• Advertisement
Sign in to follow this  

Error - list iterator not dereferencable !

This topic is 1836 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everybody! biggrin.png

When I run my program I get this error (a runtime error):

"list iterator not dereferencable"

Here is the part from my code that doing the problem. The line with the comment sign ( // )

at the start is the cause of the error.

As you can see, box.points is not an empty list and there is no divide by 0.

Thanks smile.png

box.add(Vector(5,-5,0));
box.add(Vector(8,6,0));
box.add(Vector(-4,5,0));
box.add(Vector(-5,-7,0));

for(list<Vector>::iterator i = box.points.begin(); i!= box.points.end(); ++i)
{
 float mRay = (lightpos.y - i->y) / (lightpos.x - i->x);
 for(list<Vector>::iterator j = box.points.begin(), nextDot = box.points.begin(); j!= box.points.end(); ++j)
 {
  if(nextDot!=box.points.end()) 
  {
    //nextDot++;
  }
  if(j==box.points.end()) nextDot = box.points.begin();
  float mBox = (nextDot->y - j->y) / (nextDot->x - j->x);
  if(mRay!=mBox) lightrays.push_back(Ray(lightpos, Vector(i->x,i->y,i->z)));
   }
 }

 

 

Share this post


Link to post
Share on other sites
Advertisement

The nextDot iterator is always one step ahead of the j iterator. When the j iterator is on the last element, the nextDot iterator is one past the end, and thus the dereference on line 16 fails.

 

edit: By "always", I, of course, mean at the relevant part of the loop body where the iterators are actually used; that is, line 15, 16 and 17.

Share this post


Link to post
Share on other sites
The nextDot iterator is always one step ahead of the j iterator. When the j iterator is on the last element, the nextDot iterator is one past the end, and thus the dereference on line 16 fails.

 

edit: By "always", I, of course, mean at the relevant part of the loop body where the iterators are actually used; that is, line 15, 16 and 17.

 

Thanks man but its still not working sleep.png

 

for(list<Vector>::iterator i = box.points.begin(); i!= box.points.end(); ++i)
	{
		float mRay = (lightpos.y - i->y) / (lightpos.x - i->x);
		for(list<Vector>::iterator j = box.points.begin(), nextDot = box.points.begin(); j!= box.points.end(); ++j)
		{
			if(j!=box.points.end())
			{
				nextDot++;
			}
			else 
				nextDot = box.points.begin();
			float mBox = (nextDot->y - j->y) / (nextDot->x - j->x);
			if(mRay!=mBox) lightrays.push_back(Ray(lightpos, Vector(i->x,i->y,i->z)));
		}
	}

Share this post


Link to post
Share on other sites
Line 6 can never be false. It matches the loop condition, so if it were false then the loop would have exited.
Thus your problem occurs when reaching the iterator that it just prior to end() because then nextDot will ne incremented until it equals end().
This is really the same problem as was already described to you.
The classic way of handling this kind of problem is like this (untested):
for (auto i = box.points.begin(); i!= box.points.end(); ++i)
{
	float mRay = (lightpos.y - i->y) / (lightpos.x - i->x);
	for (auto it1 = box.points.begin(), it2 = std::advance(box.points.end(), -1); it1 != box.points.end(); it2 = it1, ++it1)
	{
		float mBox = (it1->y - it2->y) / (it1->x - it2->x);
		if (mRay != mBox)
			lightrays.push_back(Ray(lightpos, Vector(i->x, i->y, i->z)));
	}
}
Pay particular attention to line 4. The key is in doing the pair that consists of the first and last item in the first iteration, then the logic to do the remaining pairs is trivial.
Note that you still need to guarantee that there are at least 2 items in the list before you execute thie bit of code, which you possibly aren't doing already. Edited by iMalc

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement