当前位置:网站首页>The wonderful use of 0 length array in C language
The wonderful use of 0 length array in C language
2022-06-27 11:06:00 【fhqlongteng】
1、0 Length array
0 Length array means that the length of the array is 0, such as int a[0], It does not take up memory space after compilation , namely sizeof(a) = 0.
C99 The regulation can be Variable length array int len ; int array[len]; In other words, the size of the array is specified only when the program runs . It is common to input data for initialization .
GCC Directly supports zero length arrays ,int a[0]; A characteristic of zero length arrays is that they do not occupy memory space .0 Length arrays are rarely used alone , It is generally used in structures , It does not occupy the length of the structure , Used to allocate contiguous memory .
2、0 The use of length array in structure
0 Length arrays are often used in structures , The following code is a structure with 0 Length array . This structure sizeof(struct rndis_query_msg) = 7 A word =28 Byte length ,OIDInputBuffer[0] Unoccupied space in the structure .
/* Remote NDIS Query Message */
struct rndis_query_msg
{
rt_uint32_t MessageType;
rt_uint32_t MessageLength;
rt_uint32_t RequestId;
rt_uint32_t Oid;
rt_uint32_t InformationBufferLength;
rt_uint32_t InformationBufferOffset;
rt_uint32_t Reserved;
rt_uint8_t OIDInputBuffer[0];
};
typedef struct rndis_query_msg* rndis_query_msg_t;OIDInputBuffer It is mainly used to access structure member variables when allocating memory Reserved Continuous memory usage later . For example, to apply for a struct rndis_query_msg Memory space occupied by structure , also OIDInputBuffer The memory pointed to has 10 Bytes of space can be used . The code can be written as follows .
rndis_query_msg_t prndis = NULL;
prndis = malloc(sizeof(struct rndis_query_msg) + 10);
if(prndis)
{
for(i = 0; i < 10; i++)
{
prndis->OIDInputBufferp[i] = i;
}
}
/* If this article helps you , Tea, please -- Add me vx:longtengweixin*/3、 Supported compilers
0 The length array is written in IAR compiler ,GCC Supported in compiler . But in keil Compiler is not supported by default , Even if the C99 The setting of is not easy to use .keil In the software, you need to add... In the compilation options --gcc Can only be compiled through .

边栏推荐
- “互联网+”大赛命题火热对接中 | 一图读懂百度38道命题
- [tcapulusdb knowledge base] Introduction to tcapulusdb data import
- Leetcode 729. 我的日程安排表 I(牛逼,已解决)
- 【TcaplusDB知识库】Tmonitor单机安装指引介绍(一)
- Mail system (based on SMTP protocol and POP3 protocol -c language implementation)
- Co jump
- Future & CompletionService
- 机器学习系统在生产中的挑战
- [tcapulusdb knowledge base] Introduction to new models of tcapulusdb
- 【TcaplusDB知识库】TcaplusDB数据构造介绍
猜你喜欢
Eureka core source code analysis
![File name setting causes an error to be written to writelines: oserror: [errno 22] invalid argument](/img/08/2d4f425e6941af35616911672b6fed.png)
File name setting causes an error to be written to writelines: oserror: [errno 22] invalid argument

【TcaplusDB知识库】TcaplusDB运维单据介绍

居家办公竟比去公司上班还累? | 社区征文

NVME2.0协议——新特性

如何在 Methodot 中部署 JupyterLab?

【TcaplusDB知识库】Tmonitor单机安装指引介绍(二)

杰理之串口通信 串口接收IO需要设置数字功能【篇】
![[worthy of collection] centos7 installation MySQL complete operation command](/img/23/7c4b69e1abc3a3ceba9b79cebe1c9b.png)
[worthy of collection] centos7 installation MySQL complete operation command

Feedforward feedback control system design (process control course design matlab/simulink)
随机推荐
What is the experience of telecommuting in a foreign company| Community essay solicitation
Deep understanding of happens before principle
杰理之一直喂狗会频繁开关中断导致定时器【篇】
Audiotrack and audiolinker
【TcaplusDB知识库】TcaplusDB业务数据备份介绍
Glide caching mechanism
KDD 2022 | 基于分层图扩散学习的癫痫波预测
deep learning statistical arbitrage
ECMAScript 6(es6)
[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (I)
If you find any loopholes later, don't tell China!
[tcapulusdb knowledge base] tcapulusdb tmonitor module architecture introduction
Challenges of machine learning system in production
Prevent being rectified after 00? I. The company's recruitment requires that employees cannot sue the company
[methodot topic] what kind of low code platform is more suitable for developers?
防止被00后整顿?一公司招聘要求员工不能起诉公司
实验笔记之——CARMEN (.log .clf)文件转换为rosbag
Native JS implements page scroll bar loading data and page drop-down loading content
信息学奥赛一本通 1332:【例2-1】周末舞会
NVME2.0协议——新特性