atoi() equivalent in C++

This is a discussion on atoi() equivalent in C++ within the C++ Programming forums, part of the General Programming Boards category; Yes. And the main problem of atoi() in my opinion is the absence of error checking both for overflow situations, ...

  1. #16
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    32,849
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #19
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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,077
    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
    Portugal
    Posts
    7,581
    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)
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,344
    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,077
    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,344
    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,236
    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

Page 2 of 2 FirstFirst 12
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-23-2009, 12:55 AM
  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-18-2005, 12:17 AM
  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, 01:17 AM

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