保留errno的值 [英] Preserving the value of errno

查看:107
本文介绍了保留errno的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在编写一个函数,如果出现问题,将设置

errno的值但是我还要

想要保证errno将保留如果函数成功完成任务,则
不变。所以在

我的代码的开头我有类似

int errsto = errno;

并且在每次成功返回之前我都有

errno = errsto;


这样可以吗?

解决方案

Spiros Bousbouras写道:


假设我正在编写一个函数,如果出现问题,将设置

errno的值但是我还要

想要保证errno保持不变

如果该功能成功完成了它的任务。所以在

我的代码的开头我有类似

int errsto = errno;

并且在每次成功返回之前我都有

errno = errsto;


这样可以吗?



为什么不把errno设置为你的错误什么东西回来之前

出错?

-

Pietro Cerutti


PGP公钥:
http://gahr.ch/pgp


Pietro Cerutti写道:


Spiros Bousbouras写道:


> ;假设我正在编写一个函数,如果出现问题,将设置errno的值但是我还要保证errno将保持不变
如果函数完成了任务成功。所以,在我的代码的开头,我有类似的东西,比如
int errsto = errno;
在每次成功返回之前我都有
errno = errsto;

这样可以吗?



为什么不在错误之前将errno设置为你的错误

出错?



!!!!!


非常好的答案。


Pietro Cerutti写道:


Spiros Bousbouras写道:


>>假设我是编写一个函数,如果出现问题,将设置errno的值但是我还要保证errno将保持不变
如果函数成功完成其任务。所以,在我的代码的开头,我有类似的东西,比如
int errsto = errno;
在每次成功返回之前我都有
errno = errsto;

这样可以吗?




为什么不在错误之前将errno设置为你的错误

出错?



我以为他想防止它被任何功能改变

他又轮流打电话......


Assume I''m writing a function which is going to set
the value of errno if something went wrong but I also
want to guarantee that errno will remain unchanged
if the function completed its task succesfully. So at
the beginning of my code I have something like
int errsto = errno ;
and just before every successful return I have
errno = errsto ;

Is this ok ?

解决方案

Spiros Bousbouras wrote:

Assume I''m writing a function which is going to set
the value of errno if something went wrong but I also
want to guarantee that errno will remain unchanged
if the function completed its task succesfully. So at
the beginning of my code I have something like
int errsto = errno ;
and just before every successful return I have
errno = errsto ;

Is this ok ?

Why not just set errno to your error before returning when something
goes wrong?
--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp


Pietro Cerutti wrote:

Spiros Bousbouras wrote:

>Assume I''m writing a function which is going to set
the value of errno if something went wrong but I also
want to guarantee that errno will remain unchanged
if the function completed its task succesfully. So at
the beginning of my code I have something like
int errsto = errno ;
and just before every successful return I have
errno = errsto ;

Is this ok ?


Why not just set errno to your error before returning when something
goes wrong?

!!!!!

Very good answer.


Pietro Cerutti wrote:

Spiros Bousbouras wrote:

>>Assume I''m writing a function which is going to set
the value of errno if something went wrong but I also
want to guarantee that errno will remain unchanged
if the function completed its task succesfully. So at
the beginning of my code I have something like
int errsto = errno ;
and just before every successful return I have
errno = errsto ;

Is this ok ?



Why not just set errno to your error before returning when something
goes wrong?

I assumed he wanted to guard against it being changed by any functions
he called in turn...


这篇关于保留errno的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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