当前位置:网站首页>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.
边栏推荐
- C# 三种方式实现Socket数据接收
- 变量与“零值”的比较
- 软考高级(信息系统项目管理师)高频考点:项目质量管理
- Improving Multimodal Accuracy Through Modality Pre-training and Attention
- Signed and unsigned keywords
- 【编译原理】做了一半的LR(0)分析器
- 做接口测试都测什么?有哪些通用测试点?
- Export MySQL table data in pure mode
- Inno Setup 打包及签名指南
- 硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
猜你喜欢
随机推荐
PVL EDI 项目案例
在IPv6中 链路本地地址的优势
Dealing with the crash of QT quick project in offscreen mode
UDP programming
2022-07-05 使用tpcc对stonedb进行子查询测试
基于 QEMUv8 搭建 OP-TEE 开发环境
CCNA Cisco network EIGRP protocol
图像的spatial domain 和 frequency domain 图像压缩
中国VOCs催化剂行业研究与投资战略报告(2022版)
2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
go多样化定时任务通用实现与封装
pytorch_ Yolox pruning [with code]
Web APIs DOM time object
signed、unsigned关键字
Inno Setup 打包及签名指南
Inno setup packaging and signing Guide
UDP编程
【编译原理】做了一半的LR(0)分析器
Attack and defense world ditf Misc
Notes de développement du matériel (10): flux de base du développement du matériel, fabrication d'un module USB à RS232 (9): création de la Bibliothèque d'emballage ch340g / max232 SOP - 16 et Associa







![[leetcode daily clock in] 1020 Number of enclaves](/img/2d/3d12f20c8c73fb28044c01be633c99.jpg)

