• FEATURED

View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Error - list iterator not dereferencable !

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

#1AgentChaos  Members

Posted 13 January 2013 - 04:03 PM

Hi everybody!

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

box.add(Vector(5,-5,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)));
}
}

#2Brother Bob  Moderators

Posted 13 January 2013 - 04:26 PM

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.

#3AgentChaos  Members

Posted 13 January 2013 - 10:44 PM

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

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)));
}
}

#4iMalc  Members

Posted 14 January 2013 - 12:52 AM

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, 14 January 2013 - 01:28 AM.

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.