atoi — how to identify the difference between zero and error?

末鹿安然 提交于 2019-11-26 04:27:34

问题


http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

Return Value

On success, the function returns the converted integral number as an int value. If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX or INT_MIN is returned.

So how I differ between atoi(\"poop\") and atoi(\"0\") and atoi(\"0000000\")

Yes I can loop and check for all zeroes in case I get 0 result, but isn\'t there a better way?

Notice: I use ANSI C89


回答1:


That's one of the reasons atoi is sometimes considered unsafe. Use strtol / strtoul instead. And if you have it use strtonum.

The function atoi is more dangerous than you might think. The POSIX standard says:

If the value cannot be represented, the behavior is undefined.

The C99 standard says this also:

7.20.1

The functions atof, atoi, atol, and atoll need not affect the value of the integer expression errno on an error. If the value of the result cannot be represented, the behavior is undefined.




回答2:


As described by @cnicutar and @ouah, atoi can't distinguish a valid 0 from an invalid string making the strtol family better options.

But Why? and How? First understand that both atoi and strtol only convert the initial set of numbers in a string to numeric values. Any trailing non-numeric characters are simply ignored. strtol can be used to check for invalid strings because in addition to a numeric value, it also returns a pointer to the end of the numeric portion of the string. Thus if this end pointer still refers to the start of the original string, you can tell that there was an error and no characters from the string were converted.

There are a few of other subtleties, as seen in the code example:

long lnum;
int num;
char *end;

errno = 0;

lnum = strtol(in_str, &end, 10);        //10 specifies base-10
if (end == in_str)     //if no characters were converted these pointers are equal
    fprintf(stderr, "ERROR: can't convert string to number\n");

//If sizeof(int) == sizeof(long), we have to explicitly check for overflows
if ((lnum == LONG_MAX || lnum == LONG_MIN) && errno == ERANGE)  
    fprintf(stderr, "ERROR: number out of range for LONG\n");

//Because strtol produces a long, check for overflow
if ( (lnum > INT_MAX) || (lnum < INT_MIN) )
    fprintf(stderr, "ERROR: number out of range for INT\n");

//Finally convert the result to a plain int (if that's what you want)
num = (int) lnum;

Note: If you are sure the input string will be within the valid int range, you can eliminate lnum and simply cast strtol's return directly: num = (int) strtolen(in_str, &end, 10);




回答3:


You cannot.

atoi cannot detect errors. If the result cannot be represented, atoi invokes undefined behavior. Use strtol instead of atoi.

Secure CERT coding advises to use strtol instead of atoi, read:

INT06-C. Use strtol() or a related function to convert a string token to an integer



来源:https://stackoverflow.com/questions/8871711/atoi-how-to-identify-the-difference-between-zero-and-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!