C或C ++中位字段的最大大小? [英] Maximum size of a bit field in C or C++?

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

问题描述

可能重复:
结构位域的最大大小(C99,C ++)

我可以在C或C ++的位字段中指定的位数是否有限制?例如,我可以这样做:

Is there a limit to the number of bits that I can specify in a bit field in C or C++? For example, could I do this:

struct HugeInt {
    int myInt: 1000;
};

我正在询问C和C ++,因为我知道语言规范有时会有所不同,并且想看看上面的示例是否可以保证在C或C ++中工作/不工作.

I'm asking about both C and C++, since I know that the language specs sometimes differ and wanted to see if the above example was guaranteed to work / not work in C or C++.

推荐答案

在C11中,第6.7.2.1节第4条:

In C11, section 6.7.2.1, clause 4:

指定位域宽度的表达式应为整数常量表达式,且其非负值不得超过冒号和省略的表达式所要指定的类型的对象的宽度.如果值为零,则声明中将没有声明符.

The expression that specifies the width of a bit-field shall be an integer constant expression with a nonnegative value that does not exceed the width of an object of the type that would be specified were the colon and expression omitted. If the value is zero, the declaration shall have no declarator.

因此,简而言之,如果它没有位字段部分,则必须在零和类型的大小之间.

So in short, it must be between zero and the size of the type if it had not had a bit-field part.

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

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