errno 的坑
来源:互联网 发布:淘宝卖的6s为什么便宜 编辑:程序博客网 时间:2024/06/05 12:42
由于 strtoll, strtoull 返回值不能标识处理是否成功,所以需要借助 errno, 看下面这段代码:
#include <stdio.h>#include <stdlib.h>#include <errno.h>int main(int argc, char* argv[]){ if (argc != 2) { printf("Usage: %s <number>\n", argv[0]); return -1; } // errno was set to some value in somewhere, but we don't know. errno = ERANGE; char * pEnd; long long value = strtoll(argv[1], &pEnd, 0); if (ERANGE == errno) printf("out of range\n"); else printf("%lld\n", value); printf("---------------\n"); // 1 -9223372036854775809 -9223372036854775808 9223372036854775807 9223372036854775808 9223372036854775807.2 return 0;}输入 1 时误判为越界,所以在用 errno 时必须调用放手动置 errno 为 0, 系统提供的 api 内部是不会去将 errno 置为 0 的。
因为 errno 是全局的,整个线程都生效,在一个很大的项目中,errno 在哪里不小心被置为非 0 值是极有可能的,因此,需要使用者非常小心。
正确的使用方法:
Since strtoul() can legitimately return 0 or LONG_MAX (LLONG_MAX for strtoull()) on both success and failure, the calling program should set errno to 0 before the call, and then determine if an error occurred by checking whether errno has a non-zero value after the call.
#include <stdio.h>#include <stdlib.h>#include <errno.h>int main(int argc, char* argv[]){ if (argc != 2) { printf("Usage: %s <number>\n", argv[0]); return -1; } // errno was set to some value in somewhere, but we don't know. errno = ERANGE; // [Note] You must set errno to 0 before calling. int errnoBackup = errno; errno = 0; char * pEnd; long long value = strtoll(argv[1], &pEnd, 0); if (ERANGE == errno) printf("out of range\n"); else printf("%lld\n", value); printf("---------------\n"); // 1 -9223372036854775809 -9223372036854775808 9223372036854775807 9223372036854775808 9223372036854775807.2 // [Note] Recovery errno. errno = errnoBackup; return 0;}
0 0
- errno 的坑
- 关于 errno的使用
- errno的启示
- errno的启示
- errno的记录
- errno的启示
- 【转载】errno的启示
- AIX下的errno
- errno的启示
- errno的用法
- linux 的errno定义
- linux的errno定义
- linux 的errno定义
- Linux errno的使用说明
- linux的errno
- errno的基本用法
- errno的启示
- errno的含义
- zzuli OJ 1004: 三位数的数位分离
- JPQL
- C# HttpWebRequest采集时出现操作操时
- 线程同步
- 新的起步,新的征程
- errno 的坑
- zzuli OJ 1005: 整数幂
- Java之——Timer与ScheduledExecutorService
- SQL 表操作小技巧(1)
- java执行Linux命令,支持通配符(*)
- 一个简单的PHP在线书签系统
- IOS UICollectionViewLayout详解
- JPA 事务管理
- android zxing