CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19
  1. #16
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: What could cause a core dump at iterator pre increment??

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  2. #17
    Join Date
    Apr 1999
    Posts
    27,449

    Re: What could cause a core dump at iterator pre increment??

    Quote Originally Posted by justin0108 View Post
    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.

  3. #18
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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.

  4. #19
    Join Date
    Sep 2003
    Posts
    213

    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!!!

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured