联盟元件排列 [英] Union element alignment

查看:133
本文介绍了联盟元件排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个工会,C标准保证了工会本身将被排列到最大元素的大小。

 工会ü{
    长升;
    INT I;
    短裤;
    字符C [2];
} U;
 

但是,它说什么了关于工会内部各个工会元件排列?如下EX pression保证是真的吗?

 (安培; u.l ==&安培; u.i)及和放大器; (安培; u.i ==&安培; U.S)及&安培; (安培; U.S ==&安培; u.c [0])
 

解决方案

每个元素的开始与工会本身的地址相一致。

这样在EX pression你问有关个人比较是真实的,但前pression作为一个整体是假的,除非工会位于地址0×0001。

适用于以下比较被删除的内容的:

 &放大器; UL ==&放大器; UI ==&放大器;我们==&放大器; UC [0]
 

修订后的版本进行比较不同的指针类型 - 指针应该转换为空指针


我被要求引用标准 - 或标识标准的部分。

C99 - 第6.7.2.1结构和联合说明(第14段):

  

一个指针,联合对象,适当转换,分给每个成员(或者,如果一个成员是位域,然后在它所在的单元),反之亦然。

If I have a union, C standard guarantees that the union itself will be aligned to the size of the largest element.

union U {
    long l;
    int i;
    short s;
    char c[2];
} u;

But what does it say about alignment of individual union elements inside the union? Is the following expression guaranteed to be true?

(&u.l == &u.i) && (&u.i == &u.s) && (&u.s == &u.c[0])

解决方案

The start of each element is aligned with the address of the union itself.

so the individual comparisons in the expression you ask about are true, but the expression as a whole is false unless the union is located at address 0x0001.

The deleted text applied to the following comparisons:

&u.l == &u.i == &u.s == &u.c[0]

The revised version compares distinct pointer types - the pointers should be cast to void pointers.


I was asked to quote the standard - or identify the section of the standard.

C99 - section 6.7.2.1 Structure and union specifiers (paragraph 14):

A pointer to a union object, suitably converted, points to each of its members (or if a member is a bitfield, then to the unit in which it resides), and vice versa.

这篇关于联盟元件排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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