数组是不是左值和顺序点限制 [英] arrays that are not lvalues and sequence point restriction

查看:140
本文介绍了数组是不是左值和顺序点限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ISO C99,不属于左值仍然衰减到指针,并且可以下标,尽管他们可能不被修改或下一个序列点后使用阵列。 (

In ISO C99, arrays that are not lvalues still decay to pointers, and may be subscripted, although they may not be modified or used after the next sequence point. (source)

据我了解,这个功能可以让数组索引的情况下,一个函数返回包含数组,这是不是在允许的C89(的 http://yarchive.net/comp/struct_return.html

I understand that this feature allows array indexing in cases where a function returning a structure containing an array, which is not allowed in C89 ( http://yarchive.net/comp/struct_return.html)

请你帮助我了解为什么会出现使用上/下一序列点之后修改它的限制?

will you please help me understand why there is a restriction on using/modifying it after the next sequence point?

推荐答案

请注意,引用文本OP是从GCC文档。从C99相关的文字来备份报价是:

Note, the text OP quoted is from GCC documentation. Relevant text from C99 to back up that quote is:

C99 6.5.2.2/5

C99 6.5.2.2/5

如果试图修改函数调用的结果或之后的下一个访问它
  序列来看,该行为是不确定的。

If an attempt is made to modify the result of a function call or to access it after the next sequence point, the behavior is undefined.

和也从前言更改列表:

数组转换为指针不限于左值

conversion of array to pointer not limited to lvalues

我没有C89文本来比较,但阵列到指针转换(6.3.2.1/3)的C99说明没有提到的数组是一个左值上的任何限制。此外,在对下标C99部分(6.5.2.1/2)谈到了前pression被下标为的前后缀pression 的,它并没有任何提及左值。

I don't have the C89 text to compare, but the C99 description of array-to-pointer conversion (6.3.2.1/3) does not mention any restriction on the array being an lvalue. Also, the C99 section on subscripting (6.5.2.1/2) talks about the expression being subscripted as postfix expression, it does not mention lvalues either.

考虑这个code:

struct foo
{
     char buf[20];
};

struct foo foo(char const *p) { struct foo f; strcpy(f.buf, p); return f; }

int main()
{
     char *hello = foo("hello").buf;
     char *bye = foo("bye").buf;

     // other stuff...

     printf("%s\n", hello);
     printf("%s\n", bye);
}

在哪里指针你好再见指向?该条款的目的是说,编译器没有把所有返回的对象在内存中闲逛,以使这些指针保持长期有效的地方。

Where do the pointers hello and bye point to? The purpose of this clause is to say that the compiler does not have to keep all of the returned objects hanging around in memory somewhere in order to make those pointers remain valid indefinitely.

相反,你好是只有等到下一个有效期; 在这种情况下(或下一个序列点一般)。这使得自由地落实价值返回结构作为一个隐藏的指针参数,克里斯托雷克在他出色的岗位,可以释放在当前语句的末尾介绍了编译器。

Instead, the hello is only valid up until the next ; in this case (or next sequence point in general). This leaves the compiler free to implement returning structs by value as a hidden pointer parameter, as Chris Torek describes in his excellent post, which can be "freed" at the end of the current statement.

NB。在克里斯的职位描述,C99的情况是不是很简单,因为有以下工作:

NB. The C99 situation isn't quite as simple as described in Chris's post, as the following has to work:

printf("%s %s\n", foo("hello").buf, foo("bye").buf);

我安装gcc 4.8似乎得到它,虽然正确的 - 以 -std = C99 的作品,并与 -std = C89 <段错误/ code>。

My install of gcc 4.8 seems to get it right though - that works with -std=c99, and segfaults with -std=c89.

这篇关于数组是不是左值和顺序点限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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