Error[8]: Undefined offset: 5, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我一直在尝试使用strtol将char数组正确转换为long,检查是否存在溢出或下溢,然后对long执行int转换.一路上,我注意到很多代码看起来像这样 if ((result == LONG_MAX || result == LONG_MIN) && errno == ERANGE){ // Handle the error} 为什么你不能只说 if(errno == ERANGE) 我一直在尝试使用strtol将char数组正确转换为long,检查是否存在溢出或下溢,然后对long执行int转换.一路上,我注意到很多代码看起来像这样
if ((result == LONG_MAX || result == LONG_MIN) && errno == ERANGE){   // Handle the error}

为什么你不能只说

if(errno == ERANGE){    // Handle the error}

根据我的理解,如果发生下溢或溢出,则在两种情况下都将errno设置为ERANGE.前者真的有必要吗?可以单独检查ERANGE是否有问题?

这就是我的代码现在的样子

char *endPtr; errno = 0; long result = strtol(str,&endPtr,10); if(errno == ERANGE) {     // Handle Error } else if(result > INT_MAX || result < INT_MIN) {    // Handle Error } else if(endPtr == str || *endPtr != '
if ( foo() == some_value_that_indicates_that_an_error_occurred )    then the value in errno applIEs to fooelse    foo succeeded and the errno must be ignored because it Could be anything
') { // Handle Error } num = (int)result; return num;

如果有前者的原因请告诉我.

解决方法 第一个代码片段是完全错误的,我稍后会解释原因,但首先我们需要一些背景知识.

errno是一个thread-local变量.当系统调用或某些库函数失败时,它将设置为非零值.系统调用成功时,它保持不变.因此它始终包含上次调用失败的错误号.

这意味着您有两个选择.在每次调用之前将errno设置为0,或者使用errno的标准习惯用法.这是标准习语的伪代码

errno = 0;result = strtol( buffer,&endptr,10 );if ( errno == ERANGE ){    // handle the error    // ERANGE is the only error mentioned in the C specification}else if ( endptr == buffer ){    // handle the error    // the conversion Failed,i.e. the input string was empty,// or only contained whitespace,or the first non-whitespace     // character was not valID}

大多数程序员都会使用标准习惯用法,因为在每次系统调用之前将errno设置为0是烦人的,重复的,烦人的,并且烦人地重复.更不用说你可能忘记在一个真实重要的地方将errno设置为0.

回到第一个代码段.这是错误的,因为strtol没有明确表示strtol失败的返回值.如果strtol返回LONG_MAX,则可能是发生错误,或者字符串实际包含数字LONG_MAX.无法知道strtol调用是成功还是失败.这意味着标准习惯用法(这是第一个代码片段试图实现的内容)不能与strtol一起使用.

要正确使用strtol,需要在调用之前将errno设置为0,如下所示

[+++]

请注意,某些实现为errno定义了其他非零值.有关详细信息,请参见适用的手册页

总结

以上是内存溢出为你收集整理的为什么你不能检查errno是否等于ERANGE?全部内容,希望文章能够帮你解决为什么你不能检查errno是否等于ERANGE?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
为什么你不能检查errno是否等于ERANGE?_C_内存溢出

为什么你不能检查errno是否等于ERANGE?

为什么你不能检查errno是否等于ERANGE?,第1张

概述我一直在尝试使用strtol将char数组正确转换为long,检查是否存在溢出或下溢,然后对long执行int转换.一路上,我注意到很多代码看起来像这样 if ((result == LONG_MAX || result == LONG_MIN) && errno == ERANGE){ // Handle the error} 为什么你不能只说 if(errno == ERANGE) 我一直在尝试使用strtol将char数组正确转换为long,检查是否存在溢出或下溢,然后对long执行int转换.一路上,我注意到很多代码看起来像这样
if ((result == LONG_MAX || result == LONG_MIN) && errno == ERANGE){   // Handle the error}

为什么你不能只说

if(errno == ERANGE){    // Handle the error}

根据我的理解,如果发生下溢或溢出,则在两种情况下都将errno设置为ERANGE.前者真的有必要吗?可以单独检查ERANGE是否有问题?

这就是我的代码现在的样子

char *endPtr; errno = 0; long result = strtol(str,&endPtr,10); if(errno == ERANGE) {     // Handle Error } else if(result > INT_MAX || result < INT_MIN) {    // Handle Error } else if(endPtr == str || *endPtr != '
if ( foo() == some_value_that_indicates_that_an_error_occurred )    then the value in errno applIEs to fooelse    foo succeeded and the errno must be ignored because it Could be anything
') { // Handle Error } num = (int)result; return num;

如果有前者的原因请告诉我.

解决方法 第一个代码片段是完全错误的,我稍后会解释原因,但首先我们需要一些背景知识.

errno是一个thread-local变量.当系统调用或某些库函数失败时,它将设置为非零值.系统调用成功时,它保持不变.因此它始终包含上次调用失败的错误号.

这意味着您有两个选择.在每次调用之前将errno设置为0,或者使用errno的标准习惯用法.这是标准习语的伪代码

errno = 0;result = strtol( buffer,&endptr,10 );if ( errno == ERANGE ){    // handle the error    // ERANGE is the only error mentioned in the C specification}else if ( endptr == buffer ){    // handle the error    // the conversion Failed,i.e. the input string was empty,// or only contained whitespace,or the first non-whitespace     // character was not valID}

大多数程序员都会使用标准习惯用法,因为在每次系统调用之前将errno设置为0是烦人的,重复的,烦人的,并且烦人地重复.更不用说你可能忘记在一个真实重要的地方将errno设置为0.

回到第一个代码段.这是错误的,因为strtol没有明确表示strtol失败的返回值.如果strtol返回LONG_MAX,则可能是发生错误,或者字符串实际包含数字LONG_MAX.无法知道strtol调用是成功还是失败.这意味着标准习惯用法(这是第一个代码片段试图实现的内容)不能与strtol一起使用.

要正确使用strtol,需要在调用之前将errno设置为0,如下所示

请注意,某些实现为errno定义了其他非零值.有关详细信息,请参见适用的手册页

总结

以上是内存溢出为你收集整理的为什么你不能检查errno是否等于ERANGE?全部内容,希望文章能够帮你解决为什么你不能检查errno是否等于ERANGE?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1257025.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存