为&放大器;错误号合法的C? [英] Is &errno legal C?
问题描述
每7.5,
[错误]扩展到修改lvalue175),有int类型,其价值是由几个库函数设置为正的错误编号。这是不确定的errno是一个宏或者与外部连接声明的标识符。如果宏定义是为了访问一个实际的对象pssed燮$ P $,或程序定义的名称错误号的标识符,行为是不确定的。
[errno] expands to a modifiable lvalue175) that has type int, the value of which is set to a positive error number by several library functions. It is unspecified whether errno is a macro or an identifier declared with external linkage. If a macro definition is suppressed in order to access an actual object, or a program defines an identifier with the name errno, the behavior is undefined.
175)的宏错误号不必是一个对象的标识符。它可能扩大到从一个函数调用产生修改的左值(例如,*错误号())。
175) The macro errno need not be the identifier of an object. It might expand to a modifiable lvalue resulting from a function call (for example, *errno()).
这我不清楚这是否足以要求错误号
不违反约束。 C语言有左值(如寄存器存储类变量,但只能是自动的让错误号
不能被定义为这样的)该&安培;
经营者违反约束
It's not clear to me whether this is sufficient to require that errno
not be a constraint violation. The C language has lvalues (such as register-storage-class variables; however these can only be automatic so errno
could not be defined as such) for which the &
operator is a constraint violation.
如果&放大器;错误号
是合法的C,就是要不断它要求
If &errno
is legal C, is it required to be constant?
推荐答案
所以§6.5.3.2p1指定
So §6.5.3.2p1 specifies
一元和放大器的操作;运营商应是一个功能标志,一个[]或目*运算符的结果,或者指定的对象不是一个位域,不使用寄存器存储类说明中声明的左值。
The operand of the unary & operator shall be either a function designator, the result of a [] or unary * operator, or an lvalue that designates an object that is not a bit-field and is not declared with the register storage-class specifier.
这我觉得,可采取意味着&放大器;左值
是罚款的左值,是不是在这两个类别。正如你提到的,错误号
不能使用寄存器存储类说明中声明,我认为(虽然我不是追引用现在检查),你不能有一个位域有型纯 INT
Which I think can be taken to mean that &lvalue
is fine for any lvalue that is not in those two categories. And as you mentioned, errno
cannot be declared with the register storage-class specifier, and I think (although am not chasing references to check right now) that you cannot have a bitfield that has type of plain int
.
所以,我认为,规范要求&放大器;(错误)
是合法的C
So I believe that the spec requires &(errno)
to be legal C.
如果&放大器;错误号是合法的C,就是要不断它需要
If &errno is legal C, is it required to be constant?
据我了解,允许点的,一部分错误号
来做个宏(它是在如glibc中的原因),就是允许它做个参考到线程本地存储,在这种情况下,它肯定不会是跨线程不变。我看不出有任何理由期待它必须是恒定的。只要值错误号
保留规定的语义,我看不出有任何理由不正当的C库无法更改&放大器;错误号
来指不同的存储器地址上的程序的过程中 - 例如通过释放每次你设定的时间重新分配后备存储错误号
。
As I understand it, part of the point of allowing errno
to be a macro (and the reason it is in e.g. glibc) is to allow it to be a reference to thread-local storage, in which case it will certainly not be constant across threads. And I don't see any reason to expect it must be constant. As long as the value of errno
retains the semantics specified, I see no reason a perverse C library could not change &errno
to refer to different memory addresses over the course of a program -- e.g. by freeing and reallocating the backing store every time you set errno
.
您可以想像维持在库设置的最后N errno值的环形缓冲区,并具有&放大器;错误号
总是指向最新的。我不认为这将是非常有用的,但我看不出它违反了规范的任何方式。
You could imagine maintaining a ring buffer of the last N errno values set by the library, and having &errno
always point to the latest. I don't think it would be particularly useful, but I can't see any way it violates the spec.
这篇关于为&放大器;错误号合法的C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!