Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Cyborg)
  • No Skin
Collapse
Brand Logo

CIRCLE WITH A DOT

  1. Home
  2. Uncategorized
  3. So much of this type of code to fix:

So much of this type of code to fix:

Scheduled Pinned Locked Moved Uncategorized
cpp
3 Posts 2 Posters 3 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • charette@mstdn.caC This user is from outside of this forum
    charette@mstdn.caC This user is from outside of this forum
    charette@mstdn.ca
    wrote on last edited by
    #1

    So much of this type of code to fix:

    for (auto iter = myvector.begin();
    iter < myvector.end();
    iter ++)
    {
    if (iter->foo)
    {
    myvector.erase(iter);
    }
    }

    Problems:
    1) Don't compare the iter to *less than* end(), it should be compared to end(). Works with std::vector, but only by luck.

    2) When you erase an item from a vector, you invalidate your iterators!

    I'm amazed this codebase got as far as it did. #cpp

    lann@piaille.frL 1 Reply Last reply
    1
    0
    • R relay@relay.infosec.exchange shared this topic on
    • charette@mstdn.caC charette@mstdn.ca

      So much of this type of code to fix:

      for (auto iter = myvector.begin();
      iter < myvector.end();
      iter ++)
      {
      if (iter->foo)
      {
      myvector.erase(iter);
      }
      }

      Problems:
      1) Don't compare the iter to *less than* end(), it should be compared to end(). Works with std::vector, but only by luck.

      2) When you erase an item from a vector, you invalidate your iterators!

      I'm amazed this codebase got as far as it did. #cpp

      lann@piaille.frL This user is from outside of this forum
      lann@piaille.frL This user is from outside of this forum
      lann@piaille.fr
      wrote on last edited by
      #2

      @charette try with a temp vector and add the good elements. Finally copy the tmp vector to myvector

      charette@mstdn.caC 1 Reply Last reply
      0
      • lann@piaille.frL lann@piaille.fr

        @charette try with a temp vector and add the good elements. Finally copy the tmp vector to myvector

        charette@mstdn.caC This user is from outside of this forum
        charette@mstdn.caC This user is from outside of this forum
        charette@mstdn.ca
        wrote on last edited by
        #3

        @lann I wasn't looking for solutions, I was complaining that this codebase I'm working with is full of serious coding mistakes.

        (See my previous posts on this topic.)

        1 Reply Last reply
        1
        0
        • R relay@relay.mycrowd.ca shared this topic on
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups