Thread: What is the negative effect for atoi function ?

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    What is the negative effect for atoi function ?

    Hi, i am the beginner of c language, may i know what is the negative effect for atoi function ? cause in some condition , it may cause segmentation error!!!


    Thank you.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Hi, i am the beginner of c language, may i know what is the negative effect for atoi function ? cause in some condition , it may cause segmentation error!!!

    The only way you're going to get a seg fault is if you pass it an invalid pointer, which shouldn't even happen in the first place. The only real shortcoming of atoi is that it doesn't tell you if the input string was invalid or not. But you could probably use strtol or sprintf if you wanted that information, though.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    But you could probably use strtol or sprintf if you wanted that information, though.
    sscanf(), maybe? ;-)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Now I haven't done C in a while but wouldn't a Scanset only guarantee input from the range specified and thereby avoid such an error? also sscanf does..... what again?
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The best functions to use are strtol(), strtoul() and strtod().

    The disadvantage of sscanf() and similar functions is that they can't report numeric overflow (like scanning 123456789012345 into a 32-bit int).
    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.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    atoi() never causes a segmentation fault.

    YOU cause a segmentation fault, by passing a bad pointer to atoi().
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM