不同大小的成员工会的内存布局? [英] Memory layout of union of different sized member?

查看:125
本文介绍了不同大小的成员工会的内存布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

typedef union epoll_data
{
  void        *ptr;
  int          fd;
  __uint32_t   u32;
  __uint64_t   u64;
} epoll_data_t;

下面 INT __ uint32_t的 4个字节,而其他8个字节。

Here int and __uint32_t are 4 bytes,while the others are 8 bytes.

当我们设置 FD INT ,它趴在第一个4字节或最后4个字节,或者它依赖于字节序?

When we set fd to an int,does it lie on the first 4 bytes or the last 4 bytes,or does it depend on endianness?

有些原因是AP preciated。

Some reason is appreciated.

推荐答案

它位于第4个字节。从C99标准§6.7.2.1/ 14:

It lies on the first 4 bytes. From the C99 standard §6.7.2.1/14:

工会的大小足以容纳最大的成员。成员至多一个的值可以存储在任何时候一个联合对象。一个指向联合对象,适当转换,分给每个成员(或者如果成员是一个位域,然后在它所在的单元),反之亦然。

The size of a union is sufficient to contain the largest of its members. The value of at most one of the members can be stored in a union object at any time. A pointer to a union object, suitably converted, points to each of its members (or if a member is a bit-field, then to the unit in which it resides), and vice versa.

这意味着,一个联盟的所有成员的地址是相同的。

This implies that the address of all members of a union is the same.

这篇关于不同大小的成员工会的内存布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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