我们可以在联合中使用指针吗?
如果没有为什么?联合优于结构的用途?
If no why? Uses of union over structure??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如果没有为什么?联合优于结构的用途?
If no why? Uses of union over structure??
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
您可以在联合中使用任何数据类型,没有限制。
至于在结构上使用并集,结构将其数据按顺序排列在内存中。这意味着它们的所有子组件都是独立的。
另一方面,联合体对其所有子组件使用相同的内存,因此一次只能存在一个。
例如:
结构用于“对象”由其他对象组成的情况,例如由两个整数组成的点对象,这两个整数是 x 和 y 坐标:
联合通常用于对象可以是许多事物之一但一次只有一个,例如无类型存储系统:
它们对于节省内存很有用,尽管现在这往往越来越不受关注(除了像嵌入式系统这样的低级工作之外),所以你不需要看到很多。
You can use any data type in a union, there's no restriction.
As to the use of unions over structures, structures lay out their data sequentially in memory. This mean all their sub-components are separate.
Unions, on the other hand, use the same memory for all their sub-components so only one can exist at a time.
For example:
Structures are used where an "object" is composed of other objects, like a point object consisting of two integers, those being the x and y coordinates:
Unions are typically used in situation where an object can be one of many things but only one at a time, such as a type-less storage system:
They are useful for saving memory although that tends to be less and less of a concern nowadays (other than in low-level work like embedded systems) so you don't see a lot of it.
那么,根据 ISO/IEC 9899:TC3(C99 标准):
简而言之,联合成员的内存空间是重叠的,并且您赋予联合成员的名称允许您读取该位置的内存大小。考虑一下:
在第一个 printf 中,我们正在做的是打印 32 位整数的 8 位部分。当然希望匿名结构中没有填充。
在第二个 printf 中?我必须逐步使用 gdb 才能理解,但我做到了:
当然,指针的大小都相同,因此分配
pq
会覆盖pp
的地址。我强烈怀疑将 32 位整数的地址取消引用为 8 位指针会打印“该位置的内容 + 32 位大小”,这对我来说碰巧是22334411.但我怀疑,在这一点上,行为是未定义的。
无论如何,这个小练习的目的是向您展示:
我应该指出,我可以看到
somenewtype
的实际用途,但看不到somepointer
- 这是一个我很确定会破坏的人为示例。Well, according to ISO/IEC 9899:TC3 (the C99 standard):
In short, the memory space of union members overlaps, and the names you give to union members allow you to read memory at that location in size. Consider:
In the first printf, what we're doing is printing out 8-bit parts of the 32-bit integer. Hoping of course for no padding in that anonymous struct.
In the second printf? I had to step through using gdb to understand, but I did:
Well of course, pointers are all the same size, so assigning
p.q
overwrites the address ofp.p
. I suspect strongly that de-referencing the address of the 32-bit integer to an 8-bit pointer is printing "whatever is at that location + 32 bits in size" which co-incidentally for me happens to be22334411
. But I suspect, at that point, the behaviour is undefined.Anyway, the point of that little exercise was to show you that:
I should point out I can see the practical use for
somenewtype
but not forsomepointer
- that was a contrived example I was pretty sure would break.