Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • 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 (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Unconventional for-loop bug

Unconventional for-loop bug

Scheduled Pinned Locked Moved Clever Code
c++regexhelp
6 Posts 3 Posters 21 Views 1 Watching
  • 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.
  • K Offline
    K Offline
    Kevin McFarlane
    wrote on last edited by
    #1

    I can't remember the precise details but instead of the standard C for-loop style: for (int i = 0; i < size; i++) he wrote: for (int i = 0; i != size; i++) Then inside the loop he was doing some loop counter manipulation, e.g., at some point incrementing by 2 so that i == size was never true and then you got an infinite loop. He used this loop style throughout. He was a former C++ developer and I think he was just applying the STL iterator pattern for (it = coll.begin(); it != coll.end(); ++it) but inappropriately to values. I did find out subsequently that he was an STL user (as I'd suspected prior to this). (BTW, the code quality as a whole was appaliing.)

    Kevin

    Z R 2 Replies Last reply
    0
    • K Kevin McFarlane

      I can't remember the precise details but instead of the standard C for-loop style: for (int i = 0; i < size; i++) he wrote: for (int i = 0; i != size; i++) Then inside the loop he was doing some loop counter manipulation, e.g., at some point incrementing by 2 so that i == size was never true and then you got an infinite loop. He used this loop style throughout. He was a former C++ developer and I think he was just applying the STL iterator pattern for (it = coll.begin(); it != coll.end(); ++it) but inappropriately to values. I did find out subsequently that he was an STL user (as I'd suspected prior to this). (BTW, the code quality as a whole was appaliing.)

      Kevin

      Z Offline
      Z Offline
      Zac Howland
      wrote on last edited by
      #2

      Kevin McFarlane wrote:

      I did find out subsequently that he was an STL user (as I'd suspected prior to this).

      Apparently not a very good one since "pure" STL users rarely write their own loops. ;P

      If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

      K 1 Reply Last reply
      0
      • K Kevin McFarlane

        I can't remember the precise details but instead of the standard C for-loop style: for (int i = 0; i < size; i++) he wrote: for (int i = 0; i != size; i++) Then inside the loop he was doing some loop counter manipulation, e.g., at some point incrementing by 2 so that i == size was never true and then you got an infinite loop. He used this loop style throughout. He was a former C++ developer and I think he was just applying the STL iterator pattern for (it = coll.begin(); it != coll.end(); ++it) but inappropriately to values. I did find out subsequently that he was an STL user (as I'd suspected prior to this). (BTW, the code quality as a whole was appaliing.)

        Kevin

        R Offline
        R Offline
        Ryan Binns
        wrote on last edited by
        #3

        Kevin McFarlane wrote:

        Then inside the loop he was doing some loop counter manipulation,

        Ack. If you ever need to change the loop counter, you shouldn't be using a for loop. Use a while loop instead. It's much more intuitive. When I see a for loop, I expect that the counter is only ever incremented on the line of the for keyword. Yes, I realise it wasn't you ;)

        Ryan

        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

        K 1 Reply Last reply
        0
        • Z Zac Howland

          Kevin McFarlane wrote:

          I did find out subsequently that he was an STL user (as I'd suspected prior to this).

          Apparently not a very good one since "pure" STL users rarely write their own loops. ;P

          If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

          K Offline
          K Offline
          Kevin McFarlane
          wrote on last edited by
          #4

          Zac Howland wrote:

          STL users rarely write their own loops.

          You mean they use for_each and so on? But I suspect most people learn STL initially by going through all the looping stuff. Better than avoiding it completely as many do. Algorithms and function objects I found mysterious for quite some time.

          Kevin

          Z 1 Reply Last reply
          0
          • R Ryan Binns

            Kevin McFarlane wrote:

            Then inside the loop he was doing some loop counter manipulation,

            Ack. If you ever need to change the loop counter, you shouldn't be using a for loop. Use a while loop instead. It's much more intuitive. When I see a for loop, I expect that the counter is only ever incremented on the line of the for keyword. Yes, I realise it wasn't you ;)

            Ryan

            "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

            K Offline
            K Offline
            Kevin McFarlane
            wrote on last edited by
            #5

            I fully agree.

            Kevin

            1 Reply Last reply
            0
            • K Kevin McFarlane

              Zac Howland wrote:

              STL users rarely write their own loops.

              You mean they use for_each and so on? But I suspect most people learn STL initially by going through all the looping stuff. Better than avoiding it completely as many do. Algorithms and function objects I found mysterious for quite some time.

              Kevin

              Z Offline
              Z Offline
              Zac Howland
              wrote on last edited by
              #6

              Kevin McFarlane wrote:

              You mean they use for_each and so on? But I suspect most people learn STL initially by going through all the looping stuff. Better than avoiding it completely as many do. Algorithms and function objects I found mysterious for quite some time.

              I was the same way. I treated vectors like normal c-style arrays. It wasn't until my data structures and algorithms class that I learned otherwise. After the first program in that class, we were no longer allowed to write our own loops. It definitely made you think about your design a little more by doing that.

              If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

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