errno设置后的生存周期小记

11 篇文章 0 订阅

参考:

Linux Man Page - errno

NAME

       errno - number of last error

SYNOPSIS

       #include <errno.h>

DESCRIPTION

       The  <errno.h>  header  file defines the integer variable errno, which is set by system calls and some library functions in the event of an
       error to indicate what went wrong.  Its value is significant only when the return value of the call indicated an error (i.e., -1 from  most
       system calls; -1 or NULL from most library functions); a function that succeeds is allowed to change errno(没太看懂).

       Valid error numbers are all nonzero; errno is never set to zero by any system call or library function(只有出错后,才会设置errno,后续检查先判断api返回值,如果出错误,再检查errno,或者使用strerror读出具体错误).

       For  some  system  calls and library functions (e.g., getpriority(2)), -1 is a valid return on success.    In such cases, a successful return
       can be distinguished from an error return by setting errno to zero before the call, and then, if the call returns a status  that  indicates
       that an error may have occurred, checking to see if errno has a nonzero value.

       errno  is  defined  by the ISO C standard to be a modifiable lvalue of type int, and must not be explicitly declared; errno may be a macro.
       errno is thread-local; setting it in one thread does not affect its value in any other thread.(线程安全,不用担心被打断后,errno被其他线程修改)

bad show:

实测可知:如果出错后,不手动重置错误码,后续会一直保留此错误 

good show:

建议读出错误码后,将此错误码赋值到临时变量,然后将errno设置为0,最后再打印出临时变量的错误信息

 

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值