是否及((结构名*)NULL - > b)安排在C11未定义的行为? [英] Does &((struct name *)NULL -> b) cause undefined behaviour in C11?

查看:138
本文介绍了是否及((结构名*)NULL - > b)安排在C11未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

code样品:<​​/ P>

Code sample:

struct name
{
    int a, b;
};

int main()
{
    &(((struct name *)NULL)->b);
}

这是否导致未定义的行为?我们可以辩论是否解引用空,然而C11并没有界定解引用。

Does this cause undefined behaviour? We could debate whether it "dereferences null", however C11 doesn't define the term "dereference".

6.5.3.2/4明确表​​示,使用 * 上一个空指针会导致未定义的行为;但它并没有说对相同 - &GT; 并且也没有定义 A - &GT; b 作为是(*一).B ;它有单独的定义为每个操作员。

6.5.3.2/4 clearly says that using * on a null pointer causes undefined behaviour; however it doesn't say the same for -> and also it does not define a -> b as being (*a).b ; it has separate definitions for each operator.

的语义 - &GT; 在6.5.2.3/4说:

The semantics of -> in 6.5.2.3/4 says:

一个后缀前pression其次 - >运算符和标识符指定成员
  的结构或联合的对象。值是该对象的到指定部件的
  其中第一八佰伴pression点,是一个左值。

A postfix expression followed by the -> operator and an identifier designates a member of a structure or union object. The value is that of the named member of the object to which the first expression points, and is an lvalue.

然而, NULL 不指向一个对象,所以第二句似乎尚未得以确认。

However, NULL does not point to an object, so the second sentence seems underspecified.

另外相关的可能是6.5.3.2/1:

Also relevant might be 6.5.3.2/1:

约束:

的操作数一元&安培; 经营者须为功能标志,的结果
   [] 或一元 * 运算符或左值,指定对象不是一个位域,是
  不使用寄存器存储类说明符声明的。

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.

不过,我觉得黑体字是有缺陷的,应当阅读的左值的潜在指定对象的,按照6.3.2.1/1(定义的左值的) - C99搞砸左值的定义,因此C11不得不重写它也许这节得到错过了。

However I feel that the bolded text is defective and should read lvalue that potentially designates an object , as per 6.3.2.1/1 (definition of lvalue) -- C99 messed up the definition of lvalue, so C11 had to rewrite it and perhaps this section got missed.

6.3.2.1/1确实说:

6.3.2.1/1 does say:

这是左值是一个前pression(比其他无效的对象类型)可能
  指定的对象;如果当被评价的左值没有指定一个对象,则
  行为是未定义

An lvalue is an expression (with an object type other than void) that potentially designates an object; if an lvalue does not designate an object when it is evaluated, the behavior is undefined

然而&安培; 操作符的评估其操作数。 (它不访问存储的价值,但是这是不同的)。

however the & operator does evaluate its operand. (It doesn't access the stored value but that is different).

这漫长推理链似乎表明,code使UB但是它是相当脆弱的,它不是很清楚,我是什么标准的编写者意。事实上,如果他们想什么,而不是留给了我们辩论:)

This long chain of reasoning seems to suggest that the code causes UB however it is fairly tenuous and it's not clear to me what the writers of the Standard intended. If in fact they intended anything, rather than leaving it up to us to debate :)

推荐答案

从一个律师的角度,前pression 及(((结构名*)NULL) - GT; b); 应引起UB,因为你无法在其中找到就没有UB的路径。恕我直言,根本原因是,在一个时刻,你应用 - 方式&gt; 运营商上的前pression不指向一个对象

From a lawyer point of view, the expression &(((struct name *)NULL)->b); should lead to UB, since you could not find a path in which there would be no UB. IMHO the root cause is that at a moment you apply the -> operator on an expression that does not point to an object.

从一个编译器来看,假设编译器的程序员没有过于复杂,很显然,我们的前pression返回相同的值 offsetof(姓名,B)就会和我pretty肯定的只要它是没有错误编译的任何现有的编译器会给出这一结果。

From a compiler point of view, assuming the compiler programmer was not overcomplicated, it is clear that the expression returns the same value as offsetof(name, b) would, and I'm pretty sure that provided it is compiled without error any existing compiler will give that result.

由于写的,我们不能责怪一个编译器,它会注意到,在您使用操作符在内的部分 - &gt;在一个前pression比不能指向一个对象(因为它为空),并发出警告或错误信息。

As written, we could not blame a compiler that would note that in the inner part you use operator -> on an expression than cannot point to an object (since it is null) and issue a warning or an error.

我的结论是,直到有所提供它只是把它的地址是合法的做提领一空指针一种特殊的一段名言,这个前pression是不是合法的C

My conclusion is that until there is a special paragraph saying that provided it is only to take its address it is legal do dereference a null pointer, this expression is not legal C.

这篇关于是否及((结构名*)NULL - &GT; b)安排在C11未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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