位字段的大小 [英] sizes of bit fields

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

问题描述

在ANSI C中,我们可以在结构内部使用一些字段,例如


struct _A {

int i:2;

};


将定义一个2位长度的变量。我怀疑那个结构大小会是什么?它是sizeof(int)还是只有1

字节?我知道它是特定于编译器的,但根据ANSI标准,它应该是什么?b $ b?我无法找到确切的文档

解释这个。


Microsoft编译器确实为这种结构提供了sizeof(int)。

问候,

-Abhishikt

In ANSI C, we can have a bit fields inside structure like

struct _A {
int i:2;
};

would define a variable of 2-bit length. My doubt is that what would
the size of this structure then? Would it be sizeof(int) or just 1
byte? I know that it is quite compiler specific, but what it should be
according to the ANSI standard? I couldn''t find exact documentation
explaining this.

Microsoft compiler does give sizeof(int) for such structure.

Regards,
-Abhishikt

推荐答案

Abhi写道:
将定义一个2位长度的变量。我怀疑那个结构的大小会是什么呢?它是sizeof(int)还是只有1
字节?
would define a variable of 2-bit length. My doubt is that what would
the size of this structure then? Would it be sizeof(int) or just 1
byte?




结构的实际大小取决于你的编译器,以及它的数量
为每个数据字分配的
空间。在gcc中,它以4个字节的块为单位分配空间
(如果你超过32位,它会分配一个新的)。所以


actual_size = size + word_size - size%word_size


这个等式没有考虑填充字段(字段

位大小为0,用于将下一个字段移动到下一个字边界。)


问候,

David。

-

David Lago< dl *** @ mundo-r.com>


PGP密钥位于: http://pgp.mit.edu

Personal Blizog: http://arcanelinux.org/blog/dlworld

Enflame项目: http://enflame.org



The actual size of the struct depends on your compiler, and which amount
of space it allocates for each word of data. In gcc, it allocates space
in chunks of 4 bytes (if you exceed 32 bits, it allocates a new one). So

actual_size = size + word_size - size % word_size

This equation does not take in account the padding fields (fields which
bit size is 0, used so as to move next field to the next word boundary).

Regards,
David.
--
David Lago <dl***@mundo-r.com>

PGP key available at: http://pgp.mit.edu
Personal Blizog: http://arcanelinux.org/blog/dlworld
The Enflame Project: http://enflame.org


你提供的等式有助于我理解。只需注意

,word_size并不总是在4的块中。这取决于我们使用的数据类型(int或char)




typedef struct {

int a:7;

int a1:1;

} AA;


typedef struct {

char b:7;

char b1:1;

} BB;


以上两种结构AA的大小为4字节,而BB有1个字节。


另一点是我们不能有变量喜欢char c:9它

会产生编译错误。 (至少在Microsoft编译器上)。


问候,

-Abhishikt

The equation you have provided assists my understanding. Just a note
that, the word_size is not always in block of 4. It depends on what
data type we use (either int or char).

typedef struct {
int a:7;
int a1:1;
} AA;

typedef struct {
char b:7;
char b1:1;
} BB;

in above two structures AA has sizeof 4 bytes, while BB has 1 byte.

and another point is that we CANNOT have a variable like "char c:9" it
generates a compiler error. (at least on Microsoft compiler).

Regards,
-Abhishikt


On Tue,2005年7月5日03:03:52 -0700,Abhi写道:
On Tue, 05 Jul 2005 03:03:52 -0700, Abhi wrote:
在ANSI C中,我们可以在结构内部有一些字段,如
struct _A {
int i:2;
};

将定义一个2位长度的变量。我怀疑那个结构的大小会是什么呢?它是sizeof(int)还是只是1
字节?我知道它是特定于编译器的,但它应该是什么呢?根据ANSI标准?我找不到确切的文件来解释这个。


标准将位字段的分配单元保留到

实现,这就是编译器特定的原因。
在结构中放置一个位字段没有什么价值(除了

可能用于特定的范围问题),当多个

位时出现值-fields打包在一起。

Microsoft编译器为这种结构提供了sizeof(int)。
In ANSI C, we can have a bit fields inside structure like

struct _A {
int i:2;
};

would define a variable of 2-bit length. My doubt is that what would
the size of this structure then? Would it be sizeof(int) or just 1
byte? I know that it is quite compiler specific, but what it should be
according to the ANSI standard? I couldn''t find exact documentation
explaining this.
The standard leaves the allocation unit for bit-fields up to the
implementation, which is why it is compiler specific.

There is little value in putting a single bit-field in a structure (except
possibly for specific range issues), the value comes when multiple
bit-fields are packed together.
Microsoft compiler does give sizeof(int) for such structure.




这是一个合理的选择。 />

劳伦斯



Which is one reasonable choice.

Lawrence


这篇关于位字段的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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