简体   繁体   English

位字段和联合大小

[英]Bit field and union size

#include<stdio.h>
main()
{
    union d
    {
        unsigned int a:1;
        unsigned int b:3;
        unsigned :0;
        unsigned int d:1;
        unsigned int e:1;
    };
    union d aa;
    aa.b=9;
    printf("d.aa.a=%d d.aa.b=%d",aa.a, aa.b);
system("pause");
}

In this question, the size of union would be different from the no of bit fields allocated for the union. 在此问题中,联合的大小将不同于为联合分配的位字段数。 Can anyone explain the difference.. and what happens to the remaining memory? 谁能解释这个区别..剩余的内存又如何处理?

Regarding the size of the type that contains bit-fields, the standard says (C11 6.7.2.1 p11): 关于包含位字段的类型的大小,标准规定为(C11 6.7.2.1 p11):

An implementation may allocate any addressable storage unit large enough to hold a bit-field. 一个实现可以分配任何足够大的可寻址存储单元以容纳位字段。

Since this is a union , and all of the members in the union only use no more than 3 bits from an unsigned int , the size of the union would be at least the size if char , plus padding as required by your system (if any). 由于这是一个union ,并且union所有成员仅使用不带unsigned int不超过3位,因此union的大小至少应为char大小,再加上系统要求的填充(如果有) )。 On many systems it will pad out each unit to the size of the type the bit-field is taken against, so in this case I would expect the size of the union to be the same as the size of an unsigned int (although that may have happened due to normal union padding requirements anyway). 在许多系统上,它会将每个单元填充到位字段所针对的类型的大小,因此在这种情况下,我希望union的大小与unsigned int的大小相同(尽管可能会还是由于正常的union填充要求而发生)。

The 0 sized bit-field is a red-herring in a union , but it has special meaning in a struct (C11 6.7.2.1 p12): 大小为0的位域在union集中是一个红鲱鱼,但在struct (C11 6.7.2.1 p12)中具有特殊含义:

A bit-field declaration with no declarator, but only a colon and a width, indicates an unnamed bit-field. 没有声明符但只有一个冒号和一个宽度的位域声明表示未命名的位域。 As a special case, a bit-field structure member with a width of 0 indicates that no further bit-field is to be packed into the unit in which the previous bit-field, if any, was placed. 作为特殊情况,宽度为0的位域结构成员表示没有其他位域要打包到放置前一个位域(如果有)的单元中。

So, if your union was a struct instead: 因此,如果您的union是一个struct则:

struct d
{
    unsigned int a:1;
    unsigned int b:3;
    unsigned :0;
    unsigned int d:1;
    unsigned int e:1;
};

Then the size of this struct would be at least 2 char s (plus any other padding if required). 然后,此struct的大小将至少为2个char (如果需要,还要加上其他填充)。 Most of the time, it would actually come out to the size of 2 unsigned int s. 在大多数情况下,它实际上是2个unsigned int的大小。

Computer architecture have certain word-length. 计算机体系结构具有一定的字长。 While declaring bit-fields can be efficient if used correctly it wouldn't be efficient if the word-length is variable. 虽然正确使用位域是有效的,但如果字长是可变的,则声明无效。 I think most compilers would pad bits for efficiency reasons. 我认为大多数编译器出于效率原因都会填充位。 In simple words the rest of the bits will be lost in most cases. 简而言之,其余比特在大多数情况下会丢失。

For simplicity suppose word length is 8 and you use bit-fields of 2 and 3 in a union. 为简单起见,假设字长为8,并且在联合中使用2和3的位域。 I use letters to denote individual bits. 我用字母表示单个位。

a b c d e f g h

The first 3 bits a, b and c will be used in my example and compiler will make sure that rest of bits are not used. 在我的示例中将使用前3位a,b和c,编译器将确保不使用其余位。

This can explain it in better words. 可以用更好的语言解释它。

However, using bit-fields in unions? 但是,在联合中使用位域吗? I am not sure that is a good idea. 我不确定这是个好主意。

Hope my explanation helped. 希望我的解释有所帮助。 Any queries? 有疑问吗?

dough! 面团! the size would be padded to 4 bytes (under most implementations) 该大小将填充为4个字节(在大多数实现中)

so "a" would refer to the first (just next to the zeroth bit :-) bit and "b" to the first three bits and "d" and "e" would both refer to the first bit all of the same 32 bit (4byte) word. 因此,“ a”指的是第一个(紧挨着第零个比特:-),“ b”指的是前三个比特,而“ d”和“ e”都指的是相同32位的第一个比特(4byte)个字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM