C11和C99中的临时对象的生存期 [英] Lifetime of temporary objects in C11 vs C99

查看:72
本文介绍了C11和C99中的临时对象的生存期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图破译注释导致C99和C11之间发生了变化。
该注释中提议的更改最终出现在C11的6.2.4:8中,即:

I am trying to decipher a note that led to a change between C99 and C11. The change proposed in that note ended up in C11's 6.2.4:8, namely:


一个非左值表达式,带有结构或联合类型,其中
结构或联合包含具有数组类型的成员(包括
递归,所有包含的结构和联合的成员)将
指向具有自动存储期限的对象,并且暂时的寿命。
其生命周期始于对表达式求值,并且其初始
值为表达式的值。当包含完整表达式或完整声明符的
评估结束时,其生存期结束。
任何试图修改具有临时生存期的对象的尝试都会导致
未定义的行为。

A non-lvalue expression with structure or union type, where the structure or union contains a member with array type (including, recursively, members of all contained structures and unions) refers to an object with automatic storage duration and temporary lifetime. Its lifetime begins when the expression is evaluated and its initial value is the value of the expression. Its lifetime ends when the evaluation of the containing full expression or full declarator ends. Any attempt to modify an object with temporary lifetime results in undefined behavior.

我了解更改的原因是必要的(可以在此处。请注意,讨论可以追溯到C11之前。但是,我不明白克拉克·纳尔逊(Clark Nelson)在写他的便条时的一句话:

I understand why the change was needed (some discussion can be found here. Note that the discussion goes back to before C11). However, what I don't understand is a side remark that Clark Nelson made in writing his note:


请注意,这种方法还声明了像
这样的示例,它在C99下是不合格的:

Please note that this approach additionally declares an example like this, which was conforming under C99, to be non-conforming:



struct X { int a[5]; } f();
int *p = f().a;
printf("%p\n", p);

我理解为什么此示例在C11下不符合要求。我特别无法理解的是它在C99下的状态如何。而且,如果它是在C99下定义的,那么该怎么做,定义地打印一个悬空指针的值?

I understand why this example is non-conforming under C11. What I specifically fail to understand is how it is conforming under C99. And, if it is defined under C99, what is it supposed to do then, definedly print the value of a dangling pointer?

推荐答案

我的理解是,在C99中,对象的生命周期最细微的时间就是块。因此,尽管6.5.2.2(以及您所指的注释中提到的其他一些§)明确表示您无法在下一个序列点之后访问返回的值,但从技术上讲,它的地址不确定离开封闭块后(尽管您应该为读者保留一些为什么要为无法访问的对象保留一些存储空间的原因)。因此,类似

My understanding is that in C99, the finest grain of lifetime for an object is the block. Thus, while 6.5.2.2 (and some other § mentioned in the note you refer to) specifically says that you can't access the returned value after the next sequence point, technically its address is not indeterminate until after you have left the enclosing block (the reason why you should have some storage reserved for an inaccessible object is left as an exercise for the reader, though). Thus, something like

struct X { int a[5]; } f();
int *p;
{ p = f().a; }
printf("%p\n", p);

在C99和C11中均未定义。在C11中,C99中不存在的临时生存期概念允许认为一旦完整表达式结束,指针就变得不确定。

is undefined in C99 as well as in C11. In C11, the notion of "temporary lifetime", that does not exist in C99, allows to consider that the pointer becomes indeterminate as soon as the full expression ends.

这篇关于C11和C99中的临时对象的生存期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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