当前位置:网站首页>C语言 - 位段
C语言 - 位段
2022-07-06 07:57:00 【欧橘猫】
位段
- 什么是位段?
C语言允许在一个结构体中以比特位(bit)为单位来指定其成员所占内存长度
这种以位为单位的成员称为位段。利用位段能够用较少的位数存储数据。
例题:
上面的
_a : 2意思是占 2 个bit位_b : 5意思是占 5 个bit位_c : 10意思是占 10 个bit位_d : 30意思是占 30 个bit位复习一下知识:
8 bit = 1 字节
4 字节 = 一个整形(int)
上面所有成员加起来也就是 47 bit,大概就是 二个整形的大小
那答案为什么是 8 ?
那么上面的例题中的结构体中位段是怎么分配空间的呢?
位段的内存分配
- 位段的成员可以是 int unsigned int signed int 或者是 char (属于整形家族)类型
- 位段的空间上是按照需要以4个字节( int )或者1个字节( char )的方式来开辟的。
- 位段涉及很多不确定因素,位段是不跨平台的,注重可移植的程序应该避免使用位段
- 位段占的二进制位数不能超过该基本类型所能表示的最大位数
即位段不能跨字节存储。例如:int是占4个字节,那么最多只能是32 bit位,不能超过它
例题:
struct S
{
char a:3;
char b:4;
char c:5;
char d:4;
};
int main()
{
struct S n = {
0 };
n.a = 10;
n.b = 12;
n.c = 3;
n.d = 4;
return 0;
}
图解:
这是在 vs 2022编译器测试数据


- 之前说过
位段的空间上是按照4个字节( int )或者1个字节( char )的方式来开辟的
所以一次是开辟 4个int 类型的空间 - 位段在
一个字节内部的数据是先使用低地址在使用高地址,从右向左使用(char 类型)
如果是 int 类型,是一个整形从右向左 - 当一块空间里剩余的空间不够下一个成员使用时,会浪费掉
- 在位段中不给其起名字,无法访问该位段,但是计算大小是仍占空间
- 不可对位段进行 &取地址 操作
位段的跨平台问题
- int 位段被当成有符号数还是无符号数是不确定的。
- 位段中最大位的数目不能确定
例如:16位机器最大16,32位机器最大32,写成27,在16位机
器会出问题。 - 位段中的成员在内存中从左向右分配,还是从右向左分配标准尚未定义。
- 当一个结构包含两个位段,第二个位段成员比较大,无法容纳于第一个位段剩余的位时,是
舍弃剩余的位还是利用,这是不确定的。
- 总结:跟结构相比,位段可以达到同样的效果,但是可以很好的节省空间,但是有跨平台的问题存在
边栏推荐
- Get the path of edge browser
- 数据治理:微服务架构下的数据治理
- Machine learning - decision tree
- flask返回文件下载
- The Vice Minister of the Ministry of industry and information technology of "APEC industry +" of the national economic and information technology center led a team to Sichuan to investigate the operat
- Circuit breaker: use of hystrix
- MFC 给列表控件发送左键单击、双击、以及右键单击消息
- octomap averageNodeColor函数说明
- 861. Score after flipping the matrix
- File upload of DVWA range
猜你喜欢

Key value judgment in the cycle of TS type gymnastics, as keyword use

【T31ZL智能视频应用处理器资料】

Inspiration from the recruitment of bioinformatics analysts in the Department of laboratory medicine, Zhujiang Hospital, Southern Medical University

Asia Pacific Financial Media | art cube of "designer universe": Guangzhou community designers achieve "great improvement" in urban quality | observation of stable strategy industry fund

Simulation of Teman green interferometer based on MATLAB

esRally国内安装使用避坑指南-全网最新

Google may return to the Chinese market after the Spring Festival.
![[Yugong series] February 2022 U3D full stack class 011 unity section 1 mind map](/img/c3/1b6013bfb2441219bf621c3f0726ea.jpg)
[Yugong series] February 2022 U3D full stack class 011 unity section 1 mind map
![DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist](/img/17/415e66d67afb055e94a966de25c2bc.png)
DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist

A Closer Look at How Fine-tuning Changes BERT
随机推荐
Uibehavior, a comprehensive exploration of ugui source code
Simulation of Michelson interferometer based on MATLAB
23. Update data
22. Empty the table
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
"Designer universe" APEC design +: the list of winners of the Paris Design Award in France was recently announced. The winners of "Changsha world center Damei mansion" were awarded by the national eco
07- [istio] istio destinationrule (purpose rule)
【T31ZL智能视频应用处理器资料】
链表面试题(图文详解)
xpath中的position()函数使用
Asia Pacific Financial Media | "APEC industry +" Western Silicon Valley invests 2trillion yuan in Chengdu Chongqing economic circle to catch up with Shanghai | stable strategy industry fund observatio
Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
Wireshark grabs packets to understand its word TCP segment
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
Machine learning - decision tree
P3047 [USACO12FEB]Nearby Cows G(树形dp)
Onie supports pice hard disk
二叉树创建 & 遍历
[Yugong series] February 2022 U3D full stack class 011 unity section 1 mind map
[factorial inverse], [linear inverse], [combinatorial counting] Niu Mei's mathematical problems