Thread: using atoi() bad practice?

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    100

    using atoi() bad practice?

    Hey,

    I am retrieving an int value from the command line argument. I run a test using isdigit() to make sure that the value present is indeed a digit, and then I save it in a variable of type int by using atoi(argv[1]). I heard elsewhere that using atoi in this regard is not the best practice, so my question is is there a better way to do this?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You can detect errors if you use strtol.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    this is the best/cleanest I know of:

    Code:
    #include <boost/lexical_cast.hpp>
    
    int main()
    {
    
       int param = -1;
    
      try
      {
         param = boost::lexical_cast<int>(argv[1]);
      }
      catch(const boost::bad_lexical_cast &)
      {
        std::cout << "err converting param to integer. exiting..." << endl;
        return EXIT_FAILURE;
      }
    }

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    Lexical_cast is the best option as far as I know.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    or if you aren't using the Boost library, you can use std::stringstream.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Three major "problems" with atoi:

    - " 123hello" returns 123. "24Mal32" returns 24. That is, it parses from left to right, ignoring whitespaces. First non whitespace must be a digit or - or +. It then starts interpreting digits until it hits a non digit at which time it returns what it parsed until that point.

    - Failure to interpret the string returns 0. That is, you'll never know if you get 0 because it actually parsed "0" or because it failed to parse.

    - If the parsed number is out of accepted values for int, it returns INT_MAX or INT_MIN, if negative. Again you don't know if you are getting this because it parsed a number successfully or because it was out of accepted value ranges.

    However, atoi() is not "not best practice". Your requirements dictate if it is.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Three major "problems" with atoi:
    1) That's how strtol works as well. It's not a problem as much as a design decision. How would you rather it work?

    2) That's how strtol works as well. The only difference is that atoi is too restricted for you to check errors.

    3) No, it's not required to do anything of the sort. The result is undefined behavior.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bad practice or acceptable?
    By Noose in forum C++ Programming
    Replies: 6
    Last Post: 06-09-2004, 01:43 AM
  2. Replies: 11
    Last Post: 11-13-2002, 01:29 PM
  3. Bad Practice??
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 08:37 AM
  4. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 07:31 AM
  5. Bad code or bad compiler?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 10-22-2001, 09:08 PM