Thread: atoi() equivalent in C++

  1. #16
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Yes. And the main problem of atoi() in my opinion is the absence of error checking both for overflow situations, but also regarding the contents of the string.

    It stops at the first non numeric character and returns an int. Seems simple but it's actually a quiet strange function:

    Code:
    int foo = atoi("643bar") // returns 643. No error checking.
    int foo = atoi("bar") // return 0(!!). No error checking.
    int foo = atoi("0")  // obviously returns 0 too.
    There's no error with the third atoi() call. But it poses a serious problem... there's no user-made error checking possible when the atoi parameter can be 0.
    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.

  2. #17
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Actually I like that it does this:
    Code:
    int foo = atoi("643bar") // returns 643. No error checking.
    Usually there's no need to do anything when 0 is returned. But yes, this can be a problem.

    Using atoi() and itoa() is a bad idea...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This is the overflow I was talking about
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<errno.h>
    int main() {
      char *big = "12345678901234567890";
      int i = atoi(big);
      perror( "Anything?" );
      printf( "%s = %d\n", big, i );
      return 0;
    }
    
    $ gcc -W -Wall -ansi -pedantic -O2 new.c
    $ ./a.exe
    Anything?: Numerical result out of range
    12345678901234567890 = 2147483647
    http://www.hmug.org/man/3/atoi.php
    Some key phases
    "The atoi() function has been deprecated by strtol()"
    "The function atoi() need not affect the value of errno on an error."

    My implementation would seem to use the strtol() equivalence, so your mileage may vary.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You can only make atoi() or itoa() "fail", if you make a serious bug in your program...
    atoi is a serious bug in your program. Short of a complete integer validation on the string before calling atoi (which I've only seen in my own code), you're risking undefined behavior every time you use it.
    My best code is written with the delete key.

  5. #20
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    So, which do you think is better to use in C++, stringstreams or strtol()?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #21
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Stringstreams, but as others said once lexical_cast makes its way into the standard, it will be the best option by far.
    Sent from my iPadŽ

  7. #22
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I would agree lexical_cast or stringstreams. However, truth be told, for simple conversions where the string is well contained within accepted values, strtol can become a more practical option. (at least until lexical_cast doesn't make it to the standard)
    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.

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    And lexical_cast basically uses stringstreams internally itself.

  9. #24
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Daved
    And lexical_cast basically uses stringstreams internally itself.
    Does it? I've never had the pleasure of seeing the code myself. Does it actually use stringstreams or just the same background logic as stringstreams? I guess once it's standard that may just depend on your compiler's implementation.
    Sent from my iPadŽ

  10. #25
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Right, it will depend on the implementation. The boost version I have uses basic_stringstream unless your compiler doesn't support stringstreams, in which case it uses strstreams.

  11. #26
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Quote Originally Posted by MSDN
    In Visual C++ 2005, in the case of overflow with large negative integral values, LONG_MIN is returned. atoi and _wtoi return INT_MAX and INT_MIN on these conditions. In all out-of-range cases, errno is set to ERANGE. If the parameter passed in is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, these functions set errno to EINVAL and return 0.
    So it is MS specific. I didn't know that.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble with atoi
    By NewbGuy in forum C Programming
    Replies: 2
    Last Post: 05-22-2009, 11:55 PM
  2. Pointer equivalent to array notation
    By bekkilyn in forum C Programming
    Replies: 4
    Last Post: 12-06-2006, 08:22 PM
  3. Replies: 10
    Last Post: 08-17-2005, 11:17 PM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM