当前位置:网站首页>(homework) C language questions: structural alignment number, bit segment, Consortium
(homework) C language questions: structural alignment number, bit segment, Consortium
2022-07-21 09:15:00 【dfnsyyds】
1. Structure alignment number
The structure occupancy diagram is from 0 Start drawing , Then the memory occupied by each variable needs to be aligned with the alignment number , Choose with several pairs min( System alignment number , Variable size )
2.
Be careful : Now? 32 On the bit machine (X86),C Linguistic long and int All are 4 byte , Represents long integer and integer , On the old machine 4、2 Byte differentiation .C Language only stipulates sizeof Of long》=int It is generally believed that 4.
vs Of 32 and 64 Next ,long All are 4
And you can choose the second question at a glance A, Second position , Overall structure size , To divide the compiler alignment number .
pragma pack(4): Compilation options ,4 Byte alignment .
3. Bit segment
memories : The bits of the bit segment are bit position . Bit segments open up space , Develop by type , The variable in the structure is int, open 4 Bytes .
This question , It's all unsigned char type , One open 1 Bytes ,8 position .
Open sequence bit :1,(Env、Paral)、1、1, A common need 3 Bytes .
pit :
This problem pit is replacing define Of A+B, In the code sizeof Back , Just replace , So in the end sizeof(Recode)2+3
The result is :32+3 = 9
4.
memories :memset():C Initializing an array or structure in language 0 The fastest way , Because it operates directly on memory .
memset(a, 0, sizeof(a)); Directly from 0 To size a Dispose of as 0
- analysis :
- tag Is a bit segment :ucP1 byte , Total below 1, common 2.
- puc Change to bit segment type , Forget the Strong rotation represents the available space
- In limine , apply puc[4], Will apply for 4 byte .
When it becomes stronger, it turns into tagPIM Give it to pst after , You can move the first two bytes , Is equivalent to pst The structure pointer points to the head of this address .
But because of the structure , Only the first two bytes of it can be moved . The last two are 0
to 2:0000 0010
to 3:0000 0011
to 4:0000 0100
to 5:0000 0101
then 2 Give the first one. pim1, It's one byte , Then get :0000 0010
to 3, to bit1, Can only reach the bottom 1, And on the right side of the figure , Is the last of the second byte , Because use low address first , Then use high , So from right to left .
to 4, take bit2,00
to 5, Nabit 3,101,
Last :0000 0010 ,0010 1001-> 2,41
Output is 16 Base number ,2 Representatives should take at least two :02, 29,0,0
5.
First , The size of the consortium is a larger value ,short Array , In the end 14,int yes 4, So choose 14,
But to Pay attention to the alignment number .
short The alignment number of is 2,int yes 4, Finally, the size should be aligned , So we should 16.
6.
This anonymous definition , The resulting consortium or structure , Only once .
analysis :
- Open up space : Align numbers 2,1, Press the maximum :2 byte . It also conforms to the alignment
- to i[0] 0x39 、i[1] 0x38,i[0] In the array , It must be a low address , and i[1] It's a high address .
- When we use short Of k When taking out ,short It's a short integer , Even if the 16 Base number , It is also presented by the left high address and the right address .
therefore : 3839 .
commonly , The small end model ( Lower the address of low-order data , High order data is put to high address ), The high address is on the left , The low address is on the right . Even if it conforms to our normal habit of looking at numbers .
7.
The default value of enumeration is 0 Start adding 1, here we are Z1 after ,A1、B1 Will add 1
8
This question VS2013, Nothing special , Follow 19、22 almost :
边栏推荐
猜你喜欢
7.16 - 每日一题 - 408
密码输入框And优惠券And自定义软键盘
7.14 - 每日一题 - 408
The IPO of minshi group was terminated: Qin ronghua, who had planned to raise 6.5 billion yuan, was punished by the court
Online communication - knowledge driven multi strategy and multi-modal question and answer Technology Practice (Qingyuan talk, issue 22, Wang haofen)
(affectation) c langue: numéro d'alignement de la structure, segment de position, Consortium
判断View滑入或滑出屏幕可见区
C语言:文件操作:顺序读写、随机读写
Security best practices for Klocwork deployment
(作业)C语言做题:结构体对齐数、位段、联合体
随机推荐
Kotlin 函数魔法 – lambda 表达式
In microservices * IML file deletion
可观测性提升软件工程质量,观测云出席2022 QECon全球软件质量&效能大会
Win system operation and maintenance
三伏天的食物
Redis - detailed explanation of redis cli management tool
Scala advanced (VIII): collection content summary (Part 2)
ICML2022奖项公布:15篇杰出论文,复旦、厦大、上交大研究入选
7.20 - 每日一题 - 408
计协C语言第四次培训 练习题
文章阅读总结:视频理解论文串讲(Li Mu+ Yi zhu)
土巴兔IPO被终止:年利润下降33% 经纬红杉是股东
WireShark 简介和抓包原理及过程
Draw a callout box according to Yolo TXT label data
Online communication - knowledge driven multi strategy and multi-modal question and answer Technology Practice (Qingyuan talk, issue 22, Wang haofen)
ICML 2022 | 解决CNN固有缺陷, CCNN凭借单一架构,实现多项SOTA
Bubble sort and selection sort
This ide plug-in 3.0 makes you the most security aware programmer in the company
如何更好地使用笔记本的触控板(Touchpad)
(affectation) c langue: numéro d'alignement de la structure, segment de position, Consortium