-
October 16th, 2014, 08:51 AM
#16
Re: What could cause a core dump at iterator pre increment??
Originally Posted by 2kaud
Yes - and then some 'bright' programmer comes along and changes the type of container from one that doesn't invalidate used iterators to one that does. Ah!!!!!!
That would be a problem, yes. But mindlessly treating "all iterators for that container are invalid after an erase" as invalid is not a best practice to defend against such a 'bright' programmer. Code review is. After all, for some containers, insertions can render all iterators invalid. In the case of std::vector, a reserve could be used to avoid this... so, ignore the reserve and insist that the iterators should be treated as invalid?
Furthermore, in some cases it is precisely because of the properties of iterator invalidation (or not) that it is selected. Changing the container should not be done lightly, and if your 'bright' programmer did so, then it should be discovered under code review.
-
October 16th, 2014, 10:19 AM
#17
Re: What could cause a core dump at iterator pre increment??
Originally Posted by justin0108
Erasing of iterator in loop seems like a very common function to implement
Unless it is a container such as std::map, you should strive to not erase elements in a container while you're looping over the same container. I have seen too many code examples where the coder is attempting to erase while looping over a container, and invariably it fails at some point (and I am not talking just about beginner programmers, but also some professional programmers).
What you should try to do is arrange the "erased" items to a particular area of the container (usually at the end), and then call a single function to remove those elements. An example of this idiom is the remove(_if)/erase idiom used by sequence containers such as std::vector. Using this method, there is little to no chance of encountering an invalid iterator issue.
For your case, std::map doesn't have such facility, but if you're using one of the containers that has the ability to do the "remove/erase" idiom, that IMO is what should be used.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 16th, 2014 at 10:23 AM.
-
October 16th, 2014, 11:11 AM
#18
Re: What could cause a core dump at iterator pre increment??
erase remove is stable, but requires o(n) writes on average.
If you don't need a stable erase, and have only few elements to remove, then "swap with back and pop" is also always a good choice.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
October 18th, 2014, 05:40 AM
#19
Re: What could cause a core dump at iterator pre increment??
Thank you guys for all the valuable advice and discussion.
I simply LOVE this forum!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|