Thread: Preventing overflow

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Preventing overflow

    Suppose I want to accept input of an int variable. I would like the program to quit if the number input is beyond the range of the int data type. What would be the best way to do this?

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    It depends on the input of the variable. Can it be represented as a hexadecimal value? Can it be negative? Assuming length of input string can be formatted (0x, etc) and inifite, no pure and simple solution comes to my mind.

    You can load this into a larger data type and then check, but I think that the best solution would be a custom string parser.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    This is what I tried. The number is an unsigned long, and for some reason, this conditional statement is not triggered.

    Code:
        if (x < 0 || x > 4294967295) {
            cout <<" Number has to be from 0 to 4,294,967,295.";
            return -1;
        }

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Because size of long = int in this case.

    Think, if x is unsigned, how can it be less than 0..

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Yeah, I was trying to restrict the possible size of input to be processed, but still, I don't know what to do if something like a negative number is input. Something beyond cin has to intercept the input...

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can first read this into a string.

    Then perform a conversion using a stringstream. Then you can perform all kinds of checks on the input.

    Code:
    #include <string>
    #include <iostream>
    #include <sstream>
    #include <cctype>
    
    bool read_ulong(unsigned long& out)
    {
        std::string input;
        std::cin >> input;
        if (input[0] == '-') { //streams will happily read negative values into unsigned types
            return false;
        }
        std::stringstream ss(input);
        if (! (ss >> out) ) {  //input is not a number at all
            return false;
        }
        if (!ss.eof()) {
            char ch;
            ss >> ch;
            return !isdigit(ch);  //at least one unread digit - overflow
        }
        return true;
    }
    
    int main()
    {
        unsigned long n;
        while (true) {
            std::cout << "Enter unsigned integer: ";
            if (read_ulong(n)) {
                std::cout << n << '\n';
                break;
            }
            else {
                std::cout << "Bad input\n";
            }
        }
    }
    Example run:
    Code:
    Enter unsigned integer: abc
    Bad input
    Enter unsigned integer: -123
    Bad input
    Enter unsigned integer: 123456678901234567890
    Bad input
    Enter unsigned integer: 453.232
    453
    As you can see, this truncates rational numbers, which you can also report as an error or accept.
    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).

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Thanks! I would use this, but I wish there was a simpler way which only uses the string and iostream libraries.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by 843 View Post
    Thanks! I would use this, but I wish there was a simpler way which only uses the string and iostream libraries.
    Technically speaking, that's all what anon showed you really is. stringstream derived from the iosbase object. Not really what you're asking for, but that's the truth.

    You could always use strtoul(), which does actually report overflow errors, specifically.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overflow and range checking for mul/div
    By Elysia in forum C++ Programming
    Replies: 28
    Last Post: 06-06-2008, 02:09 PM
  2. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 03:20 PM
  3. Signed Char Overflow
    By coder8137 in forum C Programming
    Replies: 5
    Last Post: 11-17-2006, 08:25 AM
  4. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  5. Buffer overflow errors
    By EvBladeRunnervE in forum C Programming
    Replies: 2
    Last Post: 03-17-2004, 04:58 PM