加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 给定结构的大小

发布时间:2020-12-16 03:42:40 所属栏目:百科 来源:网络整理
导读:struct x{ char a : 1; // statement 1 char c : 3; // statement 2}; 如果这个结构将是什么大小.声明1和2的含义是什么? 解决方法 这些语句声明了Bit字段. 它意味着占用1位的存储器而c占用3位的存储器. 结构的大小将是: 至少4位 填充(比特) 最有可能的是,
struct x
{
    char a : 1; // statement 1
    char c : 3; // statement 2
};

如果这个结构将是什么大小.声明1和2的含义是什么?

解决方法

这些语句声明了Bit字段.
它意味着占用1位的存储器而c占用3位的存储器.

结构的大小将是:
至少4位
填充(比特)

最有可能的是,它将是8位,即:1个字节

因为,
如果一系列位字段没有达到int的大小,则可以进行填充.填充量由结构构件的对准特征确定.

什么是比特字段?
From IBM documentation:

Both C and C++ allow integer members to be stored into memory spaces smaller than the compiler would ordinarily allow. These space-saving structure members are called bit fields,and their width in bits can be explicitly declared. Bit fields are used in programs that must force a data structure to correspond to a fixed hardware representation and are unlikely to be portable.

The syntax for declaring a bit field is as follows:

>>-type_specifier–+————+–:–constant_expression–;—><
‘-declarator-‘

A bit field declaration contains a type specifier followed by an optional declarator,a colon,a constant integer expression that indicates the field width in bits,and a semicolon. A bit field declaration may not use either of the type qualifiers,const or volatile.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读