Thread: What happens if...?

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question What happens if...?

    What happens if you, the programmer, or the user of the program, either intentionally or accidentally attempt to store a value greater than an int type is able to handle? For example, say you had an int with byte size 4 bytes, and you attempted to store a value that could not fit inside 4 bytes only (i.e. it needs more bytes). What would happen?

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    The variable "rolls over". For a simple example of this run this code:
    Code:
    for(unsigned char charCounter = 250; ; charCounter++)
    {
         printf("charCounter is %d\n", (int)charCounter);
    }
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I may be mistaken, but the result of overflow with signed integers is undefined behavior, and with unsigned types they "wrap around" (modulo arithmetics).

    If the user enters values too big, then I suppose the result depends entirely on how input is validated, and if you use a literal value too big for the type as a programmer, the compiler might warn of this.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I think anon and jeffcobb are both right. jeffcob describes the typical behavior, but technically the result is undefined.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by anon View Post
    I may be mistaken, but the result of overflow with signed integers is undefined behavior, and with unsigned types they "wrap around" (modulo arithmetics).

    If the user enters values too big, then I suppose the result depends entirely on how input is validated, and if you use a literal value too big for the type as a programmer, the compiler might warn of this.
    It's effect on your application may be undefined, but the results are very well defined. It rolls over and sets the carry flag (CF).

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    I've found in my programs that if the user puts a number larger than 2147483647 (32 bits with all but the leftmost bit as 1) into an int (using cin>>), the program skips over any cin>> anywhere else in the program. This usually causes my programs to get stuck in an infinite loop, because every cin>> has input validation, which continually fails, and the program asks the user for input again but never waits for it to be entered.

    Why this happens, I have no idea. But nothing I've made so far has any reason for a number even close to 2 billion.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That's because std::cin does input validation. You'll need to check the result of an input operation, and if there was a problem "ignore" unread characters and "clear" the error bits.

    Code:
    #include <iostream>
    #include <limits>
    
    int main()
    {
        int n = 1;
        do {
            if (std::cin >> n) {
                std::cout << "Ok " << n << '\n';
            }
            else {
                std::cout << "Not Ok\n";
                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            }
        } while (n);
    }
    Last edited by anon; 12-21-2009 at 02:17 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by King Mir View Post
    I think anon and jeffcobb are both right. jeffcob describes the typical behavior, but technically the result is undefined.
    jeffcob described the behaviour of unsigned integral types. Anon described the behaviour with signed types (undefined) and unsigned types (modulo arithmetic).

    With unsigned types the maximum value is implementation defined (although the standards specify lower bounds for the maximum), but - whatever the maximum is - the arithmetic is modulo that maximum plus one.
    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, Buttercup, and read this, this, and this before posting again.

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Thank you.

Popular pages Recent additions subscribe to a feed