当前位置:网站首页>Struct, bit segment, enumeration, union
Struct, bit segment, enumeration, union
2022-07-02 22:32:00 【Hao Shaokang】
This blog mainly focuses on the use of these four custom types and some details .
Structure struct S There are three member variables in the , Respectively char,int,char type , All in all 6 Bytes , And the result shows that the size is 12 Bytes , The reason is that the structure has memory alignment rules .
offsetof Is a macro , You can calculate an offset of the structure member relative to the starting memory of the structure ( Unit is byte ), From this, we can analyze the distribution of each member variable in memory , Here's the picture , In fact, the storage of structure member variables follows the rules of memory alignment .
Structure memory alignment rules :
1. The first member is saved to the structure variable with an offset of 0 The address of
2. Other member variables need to be aligned to a number ( Align numbers ) At an integer multiple offset of , Align numbers = The compiler defaults to an alignment number and the smaller value of the member size (vs The default number of alignments in is 8)
3. The total size of the structure is an integral multiple of the maximum number of alignments ( Each member has an alignment number )
4. If the structure is nested with a structure , The nested structure is aligned to an integral multiple of its maximum alignment , The overall size of the structure is an integral multiple of the maximum alignment number ( The number of alignments with nested structures )
Why is there memory alignment ?
1. Platform reasons ( Reasons for transplantation )
Not all hardware platforms can access data at any address , Some hardware platforms can only access certain specific data at certain addresses , Otherwise, the hardware is abnormal
2. Performance reasons :
To access unaligned memory , The processor may need to make two memory accesses , The aligned memory only needs to be accessed once , Improved efficiency , Memory alignment of structures is the practice of exchanging space for time .
Bit segment :
The bit segment can be seen in the above figure struct S1 It takes up three bytes of memory space , So how is the specific memory allocated to each member , Each member variable is char type , So allocate one byte at a time . First, open up a byte space ,a Occupied 3 individual bit, b Occupied 4 individual bit, And then there were one bit, and c need 5 individual bit, Then open up another byte space , In the end c Is to occupy the remaining one bit, Occupy the second byte 4 individual bit, Or waste the remaining one bit, Occupying the second byte 5 individual bit Well ? Actually c Language itself has no rules , How to occupy depends on the compiler ( Therefore, bit segments do not have cross platform )(vs Middle is the second way ),c Occupying the second byte 5 individual bit, Three left bit, Not enough d Occupy , So open up a third byte , therefore struct S1 Co occupation 3 Bytes .
Since the a and b Are stored in the first byte , that a Is it to occupy the high position or the low position first , This is uncertain , We assume that a Occupy the low position first ,, that b Occupy a high position , The data should be 62 03 04
According to the memory, we can see that the result is the same as we expected , So when bit segment members are stored in the same byte , Occupy the low position first , Occupy a high position after .
struct S2 The member variables in are int type , So we first open up four byte space ,a It took two bit,b It takes five bit,c Occupied 10 individual bit, And then there were 15 individual bit, Not enough d Of 30 individual bit, So open up another four bytes ,d Occupy the post development 32 individual bit Medium 30 individual bit.
The advantage of bit segment is to reduce memory consumption , But there are also some problems with bit segments
The cross platform problem of bit segment :
1.int It's uncertain whether a bit segment is treated as a signed number or an unsigned number
2. The number of the largest bits in the bit segment cannot be determined (16 Bit machine int Occupy 16bit,32 Bit machine int Occupy 32bit)
3. The members in the bit segment are allocated from left to right in memory , Right to left distribution is still uncertain ( Two member variables occupy the high position first in the same byte , Or occupy the low position first )
4. When a structure contains two bit segments , The second member is larger , Cannot hold the remaining bits of the first bit segment , Is to discard the remaining bits , Or use , This is uncertain .
enumeration :
Enumeration types may take values MALE,FEMALE,SECRET The initial values are 0,1,2. However, the initial value can be changed
union ( Shared body ):
The special feature of the consortium is that member variables share the same memory space ,( You can see from the printed address )
The size of the consortium :
1. The size of the consortium is at least the size of the largest member
2. If the size of the largest member is not an integer multiple of the maximum alignment number , Align to an integer multiple of the maximum number of alignments
边栏推荐
- Market Research - current situation and future development trend of carob chocolate market
- 分享一下如何制作专业的手绘电子地图
- 关于PHP-数据库的 数据读取,Trying to get property 'num_rows' of non-object?
- [leetcode] sword finger offer 04 Search in two-dimensional array
- Pip install whl file Error: Error: … Ce n'est pas une roue supportée sur cette plateforme
- [C question set] of V
- ArrayList分析2 :Itr、ListIterator以及SubList中的坑
- Unity3D学习笔记4——创建Mesh高级接口
- Using emqx cloud to realize one machine one secret verification of IOT devices
- ArrayList analysis 2: pits in ITR, listiterator, and sublist
猜你喜欢
Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
Technical solution of vision and manipulator calibration system
[001] [arm-cortex-m3/4] internal register
[shutter] shutter gesture interaction (small ball following the movement of fingers)
Evolution of messaging and streaming systems under the native tide of open source cloud
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
[shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)
phpcms实现订单直接支付宝支付功能
[shutter] shutter resource file use (import resource pictures | use image resources)
Reading experience of just because
随机推荐
Market Research - current market situation and future development trend of aircraft wireless intercom system
ServiceMesh主要解决的三大痛点
一周生活
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
[Jianzhi offer] 57 And are two numbers of S
App page sharing password rails implementation
Market Research - current market situation and future development trend of third-party data platform
VIM command-t plugin error: unable to load the C extension - VIM command-t plugin error: could not load the C extension
The difference between include < > and include ""
Market Research - current situation and future development trend of carob chocolate market
Market Research - current market situation and future development trend of intravenous injection (IV) bottles
540. Single element in ordered array
将 EMQX Cloud 数据通过公网桥接到 AWS IoT
Web side defense Guide
[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
UE4 游戏架构 学习笔记
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
建立自己的网站(22)
Unity发布WebGL播放声音的一种方法
Oriental Aesthetics and software design