关于错误的atoi()的行为,我可以假设什么? [英] What can I assume about the behaviour of atoi() on error?

查看:136
本文介绍了关于错误的atoi()的行为,我可以假设什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准C库函数atoi在ISO 9899:2011中记录为:

The standard C library function atoi is documented in ISO 9899:2011 as:

7.22.1数值转换函数

1 函数atofatoiatolatoll不需要在发生错误时影响整数表达式errno的值.如果无法表示结果的值,则行为是不确定的.

7.22.1 Numeric conversion functions

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.

...

#include <stdlib.h>
int atoi(const char *nptr);
long int atol(const char *nptr);
long long int atoll(const char *nptr);

说明

2  atoiatolatoll函数分别将nptr指向的字符串的起始部分转换为intlong intlong long int表示形式.除了错误行为外,它们等同于

Description

2 The atoi, atol, and atoll functions convert the initial portion of the string pointed to by nptr to int, long int, and long long int representation, respectively. Except for the behavior on error, they are equivalent to

atoi: (int)strtol(nptr, (char **)NULL, 10)
atol: strtol(nptr, (char **)NULL, 10)
atoll: strtoll(nptr, (char **)NULL, 10)

返回

3  atoiatolatoll函数返回转换后的值.

Returns

3 The atoi, atol, and atoll functions return the converted value.

nptr指向的字符串不能解析为整数时,预期的行为是什么?似乎存在以下四种意见:

What is the intended behaviour when string pointed to by nptr cannot be parsed as an integer? The following four opinions seem to exist:

  • 不执行任何转换,并且返回零.这是一些参考文献提供的文档,例如此参考书.
  • 行为与strtol相似,但可能未设置errno.这是因为将错误行为除外"作为对§7.22.1¶1的引用.
  • 行为未指定.这就是 POSIX所说的:

  • No conversion is performed and zero is returned. This is the documentation given by some references like this one.
  • Behaviour is like that of strtol except that errno might not be set. This emerges from taking "Except for the behavior on error" as a reference to §7.22.1 ¶1.
  • Behaviour is unspecified. This is what POSIX says:

调用atoi(str)应等效于:

The call atoi(str) shall be equivalent to:

(int) strtol(str, (char **)NULL, 10)

除了错误处理可能有所不同.如果无法表示该值,则行为是不确定的.

except that the handling of errors may differ. If the value cannot be represented, the behavior is undefined.

此外,应用用法部分指出:

atoi()函数包含在strtol()中,但是由于在现有代码中已广泛使用,因此保留了该函数.如果不知道该数字在范围内,则应使用strtol(),因为不需要atoi()来执行任何错误检查.

The atoi() function is subsumed by strtol() but is retained because it is used extensively in existing code. If the number is not known to be in range, strtol() should be used because atoi() is not required to perform any error checking.

请注意,POSIX声称该规范符合ISO 9899:1999(就我而言,它包含与ISO 9899:2011相同的语言):

Note that POSIX claims that the specification is aligned to ISO 9899:1999 (which contains the same language as ISO 9899:2011 as far as I'm concerned):

此参考页上描述的功能符合ISO C标准.此处描述的要求与ISO C标准之间的任何冲突都是无意的. POSIX.1-2008的这一卷符合ISO C标准.

The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of POSIX.1-2008 defers to the ISO C standard.

根据我的本地POSIX委员会成员的说法,这是UNIX的历史行为.

According to my local POSIX committee member, this is the historical behaviour of UNIX.

行为未定义.之所以会出现这种解释,是因为§7.22.1.2¶2从未明确指出错误发生了什么.既未定义也未明确实现定义或未指定的行为.

Behaviour is undefined. This interpretation arises because §7.22.1.2 ¶2 never explicitly says what happens on error. Behaviour that is neither defined nor explicitly implementation defined or unspecified is undefined.

以下哪种解释是正确的?请尝试参考权威文档.

Which of these interpretations is correct? Please try to refer to authoritative documentation.

推荐答案

nptr指向的字符串不能解析为整数时的预期行为是什么?

What is the intended behaviour when string pointed to by nptr cannot be parsed as an integer?

请明确说明,此问题适用于

To be clear, this question applies to

// Case 1
value = atoi("");
value = atoi("  ");
value = atoi("wxyz");

而不是以下内容:

// Case 2
// NULL does not point to a string
value = atoi(NULL);
// Convert the initial portion, yet has following junk
value = atoi("123xyz");
value = atoi("123 ");

根据整数的使用情况,也许/也许不是以下情况.

And maybe/maybe not the following depending on usage of integer.

// Case 3
// Can be parsed as an _integer_, yet overflows an `int`.
value = atoi("12345678901234567890123456789012345678901234567890");


ato*()的非案例2"行为取决于


The "non-Case 2" behavior of ato*() depends on the meaning of error in

atoiatolatoll函数分别将nptr指向的字符串的起始部分转换为intlong intlong long int表示形式.除了错误上的行为外,它们等同于
atoi: (int)strtol(nptr, (char **)NULL, 10)
...
C11dr§7.22.1.22

The atoi, atol, and atoll functions convert the initial portion of the string pointed to by nptr to int, long int, and long long int representation, respectively. Except for the behavior on error, they are equivalent to
atoi: (int)strtol(nptr, (char **)NULL, 10)
...
C11dr §7.22.1.2 2


肯定地,错误包括情况3:如果正确的值超出了可表示的值的范围". strto*(),尽管可能不是ato*(),但在这种情况下,它确实设置了>中定义的错误编号errrno.由于ato*()的规范不适用于此错误,因此溢出结果为


Certainly error includes case 3: "If the correct value is outside the range of representable values". strto*(), though maybe not ato*(), in this case does set the error number errrno defined in <errno.h>. Since the specification of ato*() does not apply to this error, overflow, the result, is UB per

在本国际标准中,未定义行为用未定义行为"一词表示,或者省略了任何明确的行为定义. C11dr§42

Undefined behavior is otherwise indicated in this International Standard by the words ‘‘undefined behavior’’ or by the omission of any explicit definition of behavior. C11dr §4 2


对于情况1,strto*()的行为已得到很好的定义,但未指定会影响errno.该规范进行了详细介绍(第7.2.2.1.4 4节),并将其称为不转换",而不是错误.因此可以断言1 strto*()行为不是错误,而是没有转换".因此...


For case 1, the behavior of strto*() is well defined and is not specified to affect errno. The spec goes into detail (§7.22.1.4 4) and calls these "no conversion", not an error. So it can asserted the case 1 strto*() behavior is not an error, but a "no conversion". Thus per ...

如果无法执行转换,则返回零.C11dr§7.22.1.48

"If no conversion could be performed, zero is returned. C11dr §7.22.1.4 8

... atoi("")必须返回0.

... atoi("") must return 0.

这篇关于关于错误的atoi()的行为,我可以假设什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆