为什么可能会有工会*和结构*有区别吗? [英] Why may there be a difference between union* and struct*?

查看:109
本文介绍了为什么可能会有工会*和结构*有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C标准强制要求所有的指针为联盟活动同样进行再presentation和对齐要求。结果
它要求所有指向同一个结构秒。

因此​​,我的问题:结果
为什么标准没有强制要求指向联盟活动同样进行再presentation和对齐要求为指针,以结构 S'(我非常AP preciate利用这一点实施的一个例子。)结果
还是我只是错过相关的文字?

从标准n1570草案有关引号(C11最终稿):


  

6.2.5类型§28


  
  

一个指针无效应具有相同的重presentation和对齐要求作为
  指针为字符类型。 48)同样,指向合格的或不合格的版本
  兼容类型应具有相同的重presentation和对齐要求。 全部
  指针结构类型应具有相同的重presentation和对齐要求
  彼此。所有指向联合类型应具有相同的重presentation和
  对齐要求彼此指针到其它类型的不必具有相同的
  再presentation或对齐要求。



解决方案

无论是 1989年ANSI C的理由,也不是 ISO C99理由讨论这一点。

我怀疑有一个为缺少这样的要求任何强有力的理由。也许委员会不想强加不必要的严格要求。我也怀疑有任何真实世界的实现,其中的指针结构和指针工会的的具有相同的重presentations。

考虑,例如,一个结构可以包含单个工会成员,反之亦然,所以有可能没有很好的理由为实现使用不同的重新presentations - 也没有任何充分的理由为标准要求所有实现使用相同的重新presentations。

原因所有结构指针闻一点通是允许使用指向不完全类型。例如:

 结构foo的;无效FUNC(结构美孚*参数);结构富{/ *成员声明* /};

编译器不必知道什么结构美孚以外,这是一个结构键入知道如何生成FUNC键,()调用

这同样适用于不完全联合类型。但是,一个不完整的结构类型不能完成,工会,反之亦然,所以有在能够假设它们具有相同的重presentation没有很大的好处。

The C standard mandates that all pointers to unions have the same representation and alignment requirements.
It mandates the same for all pointers to structs.

Thus my question:
Why does the standard not mandate that pointers to unions have the same representation and alignment requirements as pointers to structs? (I would very much appreciate an example of an implementation taking advantage of this.)
Or did I simply miss the relevant text?

The relevant quote from the draft standard n1570 (C11 final draft):

6.2.5 Types § 28

A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.48) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.

解决方案

Neither the 1989 ANSI C Rationale nor the ISO C99 Rationale discusses this.

I doubt that there's any strong reason for the lack of such a requirement. Probably the committee didn't want to impose unnecessarily strict requirements. I also doubt that there are any real-world implementations where pointers to structs and pointers to unions don't have the same representations.

Consider, for example, that a struct can contain a single union member, and vice versa, so there's probably no good reason for an implementation to use different representations -- nor is there any good reason for the standard to require all implementations to use identical representations.

The reason all struct pointers "smell alike" is to permit the use of pointers to incomplete types. For example:

struct foo;

void func(struct foo *param);

struct foo { /* member declarations */ };

The compiler doesn't have to know anything about struct foo other than that it's a struct type to know how to generate a call to func().

The same applies to incomplete union types. But an incomplete struct type cannot be completed as a union, or vice versa, so there's no great benefit in being able to assume that they have the same representation.

这篇关于为什么可能会有工会*和结构*有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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