How to detect overflows within a for loop

This is a discussion on How to detect overflows within a for loop within the C Programming forums, part of the General Programming Boards category; Hi, everyone. I have the following loop: Code: unsigned int i; const unsigned int max = 10; bool ignoremax = ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    126

    How to detect overflows within a for loop

    Hi, everyone. I have the following loop:

    Code:
    unsigned int i;
    const unsigned int max = 10;
    bool ignoremax = // true or false
    for (i=1;i<max || ignoremax; i++)
    {
      printf ("This is loop number %d\n", i);
    }
    Now, what I want is to detect when i has overflowed and prevent it from wrapping around and giving misleading output within the loop. What's the most elegant way to do this? Is this acceptable or hacky?

    Code:
    unsigned int i;
    const unsigned int max = 10;
    bool ignoremax = // true or false
    for (i=1;i<max || ignoremax; if (i) i++)
    {
      if (i) printf ("This is loop number %d\n", i);
      else
        printf ("We've lost count\n");
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's usually an error to start i at 1, in C.

    Code:
    for(i=0;i<MAX;i++) {
      if(i>=BadNumber)
         break;
      array[i] = (2x + y ) * 2; //normal processing
      printf("\nThis is loop number %3d", i+1); //i starts at zero, but here, for human "consumption", it's i+1
    
    }

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Since both 'i' and 'max' are the same type and the condition is:

    Code:
    i < max
    how can 'i' become larger than max?

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    126

    ignoremax

    The test is i<max || ignoremax. My idea is that the loop goes a certain number of times, unless the user desires to have it loop infinitely. That's why i can overflow.

    Richard

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,671
    If an unsigned variable becomes zero after incrementing, overflow has occurred. Wrapping (probably better described as confirming to modulo rules) is a basic property of unsigned types.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    Well, if you're starting at 1, then you could always make your loop condition (i && (i<max || ignoremax)), then once you've lost count, and looping is unnecessary anyway, you could break from the loop.

    Really, it depends on what processing you're doing within the loop.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Richardcavell View Post
    Hi, everyone. I have the following loop:
    Now, what I want is to detect when i has overflowed and prevent it from wrapping around and giving misleading output within the loop. What's the most elegant way to do this? Is this acceptable or hacky?
    Now the bad news... You can't do that in C or any of it's cousins.

    The C language does not include range checking. It is up to you as a programmer to avoid situations where an integer overflow might occur.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to detect end of line
    By a_satari in forum C Programming
    Replies: 11
    Last Post: 03-10-2011, 11:07 AM
  2. How to Write a Program in C to detect the Ethernet Cable?
    By christyyim in forum C Programming
    Replies: 1
    Last Post: 03-11-2009, 05:12 AM
  3. Detect Running Programs?
    By MattKamil in forum Windows Programming
    Replies: 2
    Last Post: 07-03-2006, 07:13 AM
  4. Question on buffer overflows
    By maxhavoc in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2004, 03:48 PM
  5. Replies: 5
    Last Post: 11-20-2003, 01:27 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21