glibc中的strtoul的实现是否与C11标准冲突? [英] Does the implementation of strtoul in glibc conflicts with the C11 standard?

查看:238
本文介绍了glibc中的strtoul的实现是否与C11标准冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是由glibc实现的stdlib.h中的功能strtoul的描述:

The follows is the description of function strtoul in stdlib.h implemented by glibc:

功能:无符号长整数strtoul(const char *限制字符串,char **限制tailptr,int基数) 初步: MT安全区域| AS安全交流安全|请参阅POSIX安全概念.

Function: unsigned long int strtoul (const char *retrict string, char **restrict tailptr, int base) Preliminary: | MT-Safe locale | AS-Safe | AC-Safe | See POSIX Safety Concepts.

strtoul(字符串到无符号长整数")函数类似于strtol,不同之处在于它转换为无符号long int值.语法与上述strtol相同.溢出时返回的值为ULONG_MAX(请参见类型范围).

The strtoul ("string-to-unsigned-long") function is like strtol except it converts to an unsigned long int value. The syntax is the same as described above for strtol. The value returned on overflow is ULONG_MAX (see Range of Type).

如果字符串表示负数,则strtoul的作用与strtol相同,但是将结果强制转换为无符号整数.例如,这意味着strtoul在"-1"上返回ULONG_MAX,而比LONG_MIN更负的输入返回(ULONG_MAX + 1)/2.

If string depicts a negative number, strtoul acts the same as strtol but casts the result to an unsigned integer. That means for example that strtoul on "-1" returns ULONG_MAX and an input more negative than LONG_MIN returns (ULONG_MAX + 1) / 2.

如果基数超出范围,则strtoul将errno设置为EINVAL,或者在溢出时将ERGE设置为ERANGE.

strtoul sets errno to EINVAL if base is out of range, or ERANGE on overflow.

这意味着,例如,"-2"将被转换为ULONG_MAX - 1.但是C11标准[7.22.1.4-8]说:

It means that, for example, "-2" will be converted to ULONG_MAX - 1. But the C11 standard [7.22.1.4-8] says:

strtol,strtoll,strtoul和strtoull函数返回转换后的 值(如果有).如果无法执行转换,则返回零. 如果正确的值超出可表示的值的范围,则返回LONG_MIN,LONG_MAX,LLONG_MIN,LLONG_MAX,ULONG_MAX或ULLONG_MAX(根据返回类型和值的符号,如果有的话),以及宏ERANGE的值存储在errno中.

The strtol, strtoll, strtoul, and strtoull functions return the converted value, if any. If no conversion could be performed, zero is returned. If the correct value is outside the range of representable values, LONG_MIN, LONG_MAX, LLONG_MIN, LLONG_MAX, ULONG_MAX, or ULLONG_MAX is returned (according to the return type and sign of the value, if any), and the value of the macro ERANGE is stored in errno.

因此,按标准,例如,"-2"应转换为ULONG_MAX.有冲突吗?

So by the standard, for example, "-2" shall be converted to ULONG_MAX. Is it a conflict?

推荐答案

这可能是glibc在标准化发生之前实现功能的另一种情况.

This is probably another case of glibc implementing a feature before standardization occurred.

是的.

但是,我认为glibc的结果更有用.如果需要完全合规,则可以包装该函数以执行转换.

However, I consider glibc's result more useful. If you need perfect compliance, you can wrap the function to perform the conversion.

这篇关于glibc中的strtoul的实现是否与C11标准冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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