当前位置:网站首页>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.
边栏推荐
- leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
- ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
- China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
- Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"
- HDR image reconstruction from a single exposure using deep CNN reading notes
- labelimg的安装与使用
- Management background --5, sub classification
- Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
- UVa 11732 – strcmp() Anyone?
- BasicVSR_PlusPlus-master测试视频、图片
猜你喜欢
Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode
每日一题:力扣:225:用队列实现栈
Daily question 1: force deduction: 225: realize stack with queue
树的先序中序后序遍历
Mise en place d'un environnement de développement OP - tee basé sur qemuv8
signed、unsigned关键字
Assembly and interface technology experiment 5-8259 interrupt experiment
新手程序员该不该背代码?
Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
随机推荐
如何实现文字动画效果
case 关键字后面的值有什么要求吗?
ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
雅思口语的具体步骤和时间安排是什么样的?
Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
【踩坑合辑】Attempting to deserialize object on CUDA device+buff/cache占用过高+pad_sequence
如何用程序确认当前系统的存储模式?
China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
【编译原理】做了一半的LR(0)分析器
go多样化定时任务通用实现与封装
重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
PVL EDI project case
Web APIs DOM time object
柔性数组到底如何使用呢?
软考高级(信息系统项目管理师)高频考点:项目质量管理
0 basic learning C language - digital tube
ThreadLocal详解
Spatial domain and frequency domain image compression of images
rust知识思维导图xmind
That's why you can't understand recursion