当前位置:网站首页>How big is the empty structure?
How big is the empty structure?
2022-07-06 22:31:00 【It's Beichen not too PI acridine】
How big is the empty structure ?
struct keyword
struct It's a magic keyword , It packages some associated data into a whole , Easy to use .
In the network protocol 、 Communication control 、 Embedded system 、 Drive development and other places , It's not a simple byte stream that we often have to transfer (char Type of the array ), It's a combination of data , Its manifestation is a structure . Inexperienced developers tend to save all the content that needs to be delivered in order in char Type array , Transmit network message and other information through pointer offset . It's very complicated to do this , Error prone , And once the control mode and communication protocol
There are changes , The program needs to be modified very carefully , Very error prone . Only one structure is needed at this time . At ordinary times, we require that the parameters of the function should not be more than 4 individual , If the function has more arguments than 4 It's very error prone to use ( Including the meaning and order of each parameter is easy to get wrong ), Efficiency will also be reduced ( And specific CPU of ,ARM Chip for more than 4 The processing of parameters is particular , For details, please refer to the relevant materials ). This is the time , The number of parameters can be compressed with the structure .
How big is the empty structure ?
The memory size occupied by a structure is the sum of the memory occupied by its members . It's easy to understand , But the following situation ?
struct student
{
}stu;
sizeof(stu) What is the value of alpha ?
unfortunately , No 0, It is 1. Why? ? Do you think , If we put struct student As a model , Can you make a mold without any volume ? Obviously not. . So does the compiler . The compiler believes that any data type has its size , Using it to define a variable can allocate a certain size of space . In that case , The compiler takes it for granted that any structure has a size , Even if this structure is empty . Nawan
A structure is really empty , Why is its size appropriate ? Suppose there is only one in the structure char The data member of type , Then its size is 1byte( Memory alignment is not considered here ). That is to say, the non empty structure type data needs to occupy at least one byte of space , The empty structure type data cannot occupy more space than the smallest non empty structure type data . That's the trouble , The size of the empty structure cannot be 0, It can't be greater than 1, What do I do ? Defined as 0.5 individual byte? But the smallest unit of memory address is 1 individual byte,0.5 individual byte How to deal with ? The best way to solve this problem is to compromise , The compiler takes it for granted that you construct a structural data type to package some data members , And the smallest data member needs 1 individual byte, The compiler reserves at least for each structure type data 1 individual byte Space . therefore , The size of the empty structure is positioned 1 individual byte.
边栏推荐
- General implementation and encapsulation of go diversified timing tasks
- Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
- Applet system update prompt, and force the applet to restart and use the new version
- Learn the principle of database kernel from Oracle log parsing
- UDP programming
- return 关键字
- Dealing with the crash of QT quick project in offscreen mode
- Aardio - 利用customPlus库+plus构造一个多按钮组件
- qt quick项目offscreen模式下崩溃的问题处理
- const关键字
猜你喜欢
Chapter 3: detailed explanation of class loading process (class life cycle)
Aardio - 利用customPlus库+plus构造一个多按钮组件
Learn the principle of database kernel from Oracle log parsing
CCNA Cisco network EIGRP protocol
Installation and use of labelimg
第3章:类的加载过程(类的生命周期)详解
Attack and defense world miscall
【LeetCode】19、 删除链表的倒数第 N 个结点
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
二叉(搜索)树的最近公共祖先 ●●
随机推荐
基於 QEMUv8 搭建 OP-TEE 開發環境
How do I write Flask's excellent debug log message to a file in production?
Pit encountered by handwritten ABA
做国外LEAD2022年下半年几点建议
2022-07-05 使用tpcc对stonedb进行子查询测试
ThreadLocal详解
Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
做接口测试都测什么?有哪些通用测试点?
[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation
Should novice programmers memorize code?
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
CocosCreator+TypeScripts自己写一个对象池
2022-07-05 use TPCC to conduct sub query test on stonedb
Aardio - 不声明直接传float数值的方法
自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
Anaconda installs third-party packages
case 关键字后面的值有什么要求吗?
poj 1094 Sorting It All Out (拓扑排序)
China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)