当前位置:网站首页>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 时间对象
- HDR image reconstruction from a single exposure using deep CNN reading notes
- 机试刷题1
- 2022-07-05 use TPCC to conduct sub query test on stonedb
- 手写ABA遇到的坑
- Comparison between variable and "zero value"
- Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
- 树的先序中序后序遍历
- 第4章:再谈类的加载器
- Web APIs DOM time object
猜你喜欢

NetXpert XG2帮您解决“布线安装与维护”难题

Chapter 4: talk about class loader again

PVL EDI 项目案例

将MySQL的表数据纯净方式导出

Web APIs DOM time object

0 basic learning C language - interrupt

CocosCreator+TypeScripts自己写一个对象池

Export MySQL table data in pure mode

Mysql database basic operations DML

Advantages of link local address in IPv6
随机推荐
Sword finger offer question brushing record 1
[leetcode daily clock in] 1020 Number of enclaves
重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
Comparison between variable and "zero value"
go多样化定时任务通用实现与封装
Puppeteer连接已有Chrome浏览器
uniapp滑动到一定的高度后固定某个元素到顶部效果demo(整理)
2022-07-05 使用tpcc对stonedb进行子查询测试
How do I write Flask's excellent debug log message to a file in production?
如何用程序确认当前系统的存储模式?
0 basic learning C language - interrupt
Memorabilia of domestic database in June 2022 - ink Sky Wheel
What are the interface tests? What are the general test points?
UVa 11732 – strcmp() Anyone?
小程序系统更新提示,并强制小程序重启并使用新版本
MySQL数据库基本操作-DML
树的先序中序后序遍历
(十八)LCD1602实验
volatile关键字
ThreadLocal详解