当前位置:网站首页>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.
边栏推荐
- Web APIs DOM time object
- OpenCV VideoCapture. Get() parameter details
- Comparison between variable and "zero value"
- Windows Auzre 微软的云计算产品的后台操作界面
- Sword finger offer question brushing record 1
- 【雅思口语】安娜口语学习记录part1
- pytorch_YOLOX剪枝【附代码】
- 中国1,4-环己烷二甲醇(CHDM)行业调研与投资决策报告(2022版)
- Plafond du tutoriel MySQL, bien collecté, regardez lentement
- Data storage (1)
猜你喜欢
[leetcode daily clock in] 1020 Number of enclaves
Advantages of link local address in IPv6
Installation and use of labelimg
【LeetCode】19、 删除链表的倒数第 N 个结点
2021 geometry deep learning master Michael Bronstein long article analysis
config:invalid signature 解决办法和问题排查详解
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
[linear algebra] determinant of order 1.3 n
Learn the principle of database kernel from Oracle log parsing
图像的spatial domain 和 frequency domain 图像压缩
随机推荐
【数字IC手撕代码】Verilog无毛刺时钟切换电路|题目|原理|设计|仿真
Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
第4章:再谈类的加载器
柔性数组到底如何使用呢?
软考高级(信息系统项目管理师)高频考点:项目质量管理
Inno setup packaging and signing Guide
Config:invalid signature solution and troubleshooting details
uniapp滑动到一定的高度后固定某个元素到顶部效果demo(整理)
剪映+json解析将视频中的声音转换成文本
Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案
数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据
Puppeteer连接已有Chrome浏览器
2022-07-05 use TPCC to conduct sub query test on stonedb
That's why you can't understand recursion
0 basic learning C language - digital tube
General implementation and encapsulation of go diversified timing tasks
Signed and unsigned keywords
MySQL约束的分类、作用及用法
Aardio - 封装库时批量处理属性与回调函数的方法
void关键字