免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4583 | 回复: 11
打印 上一主题 下一主题

第二个关于C99标准的疑问? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-02-21 11:34 |只看该作者 |倒序浏览
6.5.2.3 Structure and union members
Constraints
1 The first operand of the . operator shall have a qualified or unqualified structure or union
type, and the second operand shall name a member of that type.
2 The first operand of the -> operator shall have type ‘‘pointer to qualified or unqualified
structure’’ or ‘‘pointer to qualified or unqualified union’’, and the second operand shall
name a member of the type pointed to.
Semantics
3 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,82) and is an lvalue if
the first expression is an lvalue. If the first expression has qualified type, the result has
the so-qualified version of the type of the designated member.
4 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.83) If the first expression is a pointer to
a qualified type, the result has the so-qualified version of the type of the designated
member.
5 One special guarantee is made in order to simplify the use of unions: if a union contains
several structures that share a common initial sequence (see below), and if the union
object currently contains one of these structures, it is permitted to inspect the common
initial part of any of them anywhere that a declaration of the complete type of the union is
visible. Two structures share a common initial sequence if corresponding members have
compatible types (and, for bit-fields, the same widths) for a sequence of one or more
initial members.
6 EXAMPLE 1 If f is a function returning a structure or union, and x is a member of that structure or
union, f().x is a valid postfix expression but is not an lvalue.
7 EXAMPLE 2 In:
struct s { int i; const int ci; };
struct s s;
const struct s cs;
volatile struct s vs;
the various members have the types:
s.i int
s.ci const int
cs.i const int
cs.ci const int
vs.i volatile int
vs.ci volatile const int

8 EXAMPLE 3 The following is a valid fragment:
union {
struct {
int alltypes;
} n;
struct {
int type;
int intnode;
} ni;
struct {
int type;
double doublenode;
} nf;
} u;
u.nf.type = 1;
u.nf.doublenode = 3.14;
/* ... */
if (u.n.alltypes == 1)
if (sin(u.nf.doublenode) == 0.0)
/* ... */
The following is not a valid fragment (because the union type is not visible within function f):
struct t1 { int m; };
struct t2 { int m; };
int f(struct t1 *p1, struct t2 *p2)
{
if (p1->m < 0)
p2->m = -p2->m;
return p1->m;
}
int g()
{
union {
struct t1 s1;
struct t2 s2;
} u;
/* ... */
return f(&u.s1, &u.s2);
}
Forward references: address and indirection operators (6.5.3.2), structure and union

红色的部分为什么说“The following is not a valid fragment (because the union type is not visible within function f):”
union在函数f中是否可见与f有什么关系呀
不明白
specifiers (6.7.2.1)

论坛徽章:
0
2 [报告]
发表于 2008-02-21 12:17 |只看该作者
union的定义在g()内,所以对于f()来说是不可见的,在f()中使用union中的元素是不行的。

论坛徽章:
0
3 [报告]
发表于 2008-02-21 12:24 |只看该作者
int f(struct t1 *p1, struct t2 *p2)
中并没有设及关于union中的任何东西。在f看来所有的都只是struct

还有在用gcc编译时也不会出现任何问题

论坛徽章:
0
4 [报告]
发表于 2008-02-21 14:53 |只看该作者
今天各位老大不知道去哪里了,是不是都去过元宵节了,祝大家元宵节快乐

论坛徽章:
0
5 [报告]
发表于 2008-02-21 15:08 |只看该作者
原帖由 zhangjiakouzf 于 2008-2-21 12:24 发表
int f(struct t1 *p1, struct t2 *p2)
中并没有设及关于union中的任何东西。在f看来所有的都只是struct

还有在用gcc编译时也不会出现任何问题


我也试了, 也没有报错.

论坛徽章:
0
6 [报告]
发表于 2008-02-21 15:45 |只看该作者
可以编译,但是逻辑有问题,因为函数f的2个参数实际指向同一个对象

论坛徽章:
0
7 [报告]
发表于 2008-02-21 16:21 |只看该作者
原帖由 ytl 于 2008-2-21 15:45 发表
可以编译,但是逻辑有问题,因为函数f的2个参数实际指向同一个对象


我觉得c99里面所指的应该不是逻辑问题,因为如果是逻辑问题的话也应该是return f(&u.s1, &u.s2);这句,因为逻辑错误是这句引起的。不关g函数什么事情呀。

论坛徽章:
0
8 [报告]
发表于 2008-02-22 10:21 |只看该作者
没人理?

论坛徽章:
0
9 [报告]
发表于 2008-02-22 10:48 |只看该作者
5. One special guarantee is made in order to simplify the use of unions: if a union contains
several structures that share a common initial sequence (see below), and if the union
object currently contains one of these structures, it is permitted to inspect the common
initial part of any of them anywhere that a declaration of the complete type of the union is
visible. Two structures share a common initial sequence if corresponding members have
compatible types (and, for bit-fields, the same widths) for a sequence of one or more
initial members.

就在说这个问题. 没有说如果看不到declaration时的行为...

论坛徽章:
0
10 [报告]
发表于 2008-02-23 14:53 |只看该作者
对于 int f(struct t1 *p1, struct t2 *p2) 这样声明的函数, C 的别名规则(Aliasing Rule)允许编译器作这样的假设前提:p1 和 p2 不指向同一对象,或者指向的对象不存在重叠。根据这个假设前提,编译器可以对函数代码进行优化(比如把 p2 指向的对象放到寄存器中)。在此情况下,f(&u.s1, &u.s2) 函数调用中两个参数指针由于实际指向同一对象,其结果有可能是不正确的。

如果在函数范围内 union u 的定义可见,则编译器可根据关于 union 的这一特殊保证(5 One special guarantee..)判断出有可能函数参数 p1 和 p2 指向同一对象 union u 的成员,从而不会对函数进行上面提到的优化,以保证函数的结果正确。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP