当前位置:网站首页>空结构体多大?
空结构体多大?
2022-07-06 14:40:00 【是北豼不太皮吖】
空结构体多大?
struct 关键字
struct 是个神奇的关键字,它将一些相关联的数据打包成一个整体,方便使用。
在网络协议、通信控制、嵌入式系统、驱动开发等地方,我们经常要传送的不是简单的字节流(char 型数组),而是多种数据组合起来的一个整体,其表现形式是一个结构体。经验不足的开发人员往往将所有需要传送的内容依顺序保存在 char 型数组中,通过指针偏移的方法传送网络报文等信息。这样做编程复杂,易出错,而且一旦控制方式及通信协议
有所变化,程序就要进行非常细致的修改,非常容易出错。这个时候只需要一个结构体就能搞定。平时我们要求函数的参数尽量不多于 4 个,如果函数的参数多于 4 个使用起来非常容易出错(包括每个参数的意义和顺序都容易弄错),效率也会降低(与具体 CPU 有关,ARM芯片对于超过 4 个参数的处理就有讲究,具体请参考相关资料)。这个时候,可以用结构体压缩参数个数。
空结构体多大?
结构体所占的内存大小是其成员所占内存之和。这点很容易理解,但是下面的这种情况呢?
struct student
{
}stu;
sizeof(stu)的值是多少呢?
很遗憾,不是 0,而是 1。为什么呢?你想想,如果我们把 struct student 看成一个模子的话,你能造出一个没有任何容积的模子吗?显然不行。编译器也是如此认为。编译器认为任何一种数据类型都有其大小,用它来定义一个变量能够分配确定大小的空间。既然如此,编译器就理所当然的认为任何一个结构体都是有大小的,哪怕这个结构体为空。那万
一结构体真的为空,它的大小为什么值比较合适呢?假设结构体内只有一个 char 型的数据成员,那其大小为 1byte(这里先不考虑内存对齐的情况)。也就是说非空结构体类型数据最少需要占一个字节的空间,而空结构体类型数据总不能比最小的非空结构体类型数据所占的空间大吧。这就麻烦了,空结构体的大小既不能为 0,也不能大于 1,怎么办?定义为 0.5个 byte?但是内存地址的最小单位是 1 个 byte,0.5 个 byte 怎么处理?解决这个问题的最好办法就是折中,编译器理所当然的认为你构造一个结构体数据类型是用来打包一些数据成员的,而最小的数据成员需要 1 个 byte,编译器为每个结构体类型数据至少预留 1 个 byte的空间。所以,空结构体的大小就定位 1 个 byte。
边栏推荐
- 硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
- NetXpert XG2帮您解决“布线安装与维护”难题
- Force deduction question 500, keyboard line, JS implementation
- 中国1,4-环己烷二甲醇(CHDM)行业调研与投资决策报告(2022版)
- Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode
- GPS from getting started to giving up (XIII), receiver autonomous integrity monitoring (RAIM)
- 3DMax指定面贴图
- Spatial domain and frequency domain image compression of images
- The nearest common ancestor of binary (search) tree ●●
- Web APIs DOM 时间对象
猜你喜欢

Adjustable DC power supply based on LM317

ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
![[sciter]: encapsulate the notification bar component based on sciter](/img/08/a3dd409261054052291e99dd28af11.png)
[sciter]: encapsulate the notification bar component based on sciter

剪映+json解析将视频中的声音转换成文本

Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing

GPS from getting started to giving up (12), Doppler constant speed

自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
![[leetcode daily clock in] 1020 Number of enclaves](/img/2d/3d12f20c8c73fb28044c01be633c99.jpg)
[leetcode daily clock in] 1020 Number of enclaves

数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据

Aardio - 不声明直接传float数值的方法
随机推荐
Four data streams of grpc
[linear algebra] determinant of order 1.3 n
Save and retrieve strings
Force buckle 575 Divide candy
Codeforces Round #274 (Div. 2) –A Expression
基于 QEMUv8 搭建 OP-TEE 开发环境
Maximum product of three numbers in question 628 of Li Kou
LeetCode 练习——剑指 Offer 26. 树的子结构
NPDP认证|产品经理如何跨职能/跨团队沟通?
[sdx62] wcn685x will bdwlan Bin and bdwlan Txt mutual conversion operation method
Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
i.mx6ull搭建boa服务器详解及其中遇到的一些问题
十二、启动流程
2022-07-05 使用tpcc对stonedb进行子查询测试
GD32F4XX串口接收中断和闲时中断配置
2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
小常识:保险中的“保全”是什么?
Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
在IPv6中 链路本地地址的优势
HDU 2008 digital statistics