Erase elements correctly when traverse a vector?

Started by
13 comments, last by GameDev.net 17 years, 4 months ago
Quote:Original post by 51mon
When I refer to elements outside the domain of an iterator, for example itrForward+1 when itrForward == vecExample.end(), the application automatically prone and I got error messages. This is a good feature most of the time but I would like to turn that off, can I do that somehow?


There are two different aspects here. The error messages are a feature of your implementation of the SC++L that you might be able to turn off. However, the fact that the application stops, crashes, or misbehaves, is a consequence of the standard, which makes incrementing iterators past the end result in undefined behavior.

So, even if you did manage to remove the helpful error messages, your code would still be prone to crashes (only they would happen at random or do more insidious things, like memory corruption).

In short, don't iterate past the end of containers.
Advertisement
Quote:Original post by ToohrVyk
Quote:Original post by 51mon
When I refer to elements outside the domain of an iterator, for example itrForward+1 when itrForward == vecExample.end(), the application automatically prone and I got error messages. This is a good feature most of the time but I would like to turn that off, can I do that somehow?


There are two different aspects here. The error messages are a feature of your implementation of the SC++L that you might be able to turn off. However, the fact that the application stops, crashes, or misbehaves, is a consequence of the standard, which makes incrementing iterators past the end result in undefined behavior.

So, even if you did manage to remove the helpful error messages, your code would still be prone to crashes (only they would happen at random or do more insidious things, like memory corruption).

In short, don't iterate past the end of containers.


Ok, I can see that but on this line for example:

while(w < (itrThis-1)->vScreenCenter.x && itrThis != vecActiveLights.begin())

Wouldn't it be safe? I can solve it with other arrangement but it save some lines and it's a real time application so time is an issue. I happen to have several locations in the code where it would be nice if I could refer to outside the domain.
while (itrThis != vecActiveLights.begin() && w < (itrThis-1)->vScreenCenter.x)

is fine assuming itrThis is valid at the start of the loop (also assuming you haven't overloaded a bunch of operators with unusual return types).

while (w < (itrThis-1)->vScreenCenter.x && itrThis != vecActiveLights.begin())

is not fine. If itrThis is vecActiveLights.begin() then (itrThis-1) points at random memory. You cannot dereference an invalid iterator and expect anything sensible. The iterator may be pointing at random memory which is not owned by your process, which is why code like this sometimes crashes.

Σnigma
Thanks that was an easy solution :)
Hungarian notation makes my poo runny, and the compiler will fix my for-loop variables if it decides to. This is modern computing, we have a real compiler and optimizer now.

This topic is closed to new replies.

Advertisement