当前位置:网站首页>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 .

边栏推荐
- Uniform Asymptotics by Alexei
- Learning notes - data set generation
- [tcapulusdb knowledge base] tcapulusdb cluster management introduction
- 直播电子商务应用程序开发需要什么基本功能?未来发展前景如何?
- 【TcaplusDB知识库】TcaplusDB集群管理介绍
- go-zero微服务实战系列(七、请求量这么高该如何优化)
- Informatics Olympiad all in one 1332: [example 2-1] weekend dance
- Memory compression for win10
- KDD 2022 | 基于分层图扩散学习的癫痫波预测
- 【TcaplusDB知识库】TcaplusDB单据受理-建表审批介绍
猜你喜欢

Tcp/ip explanation (version 2) notes / 3 link layer / 3.4 bridge and switch / 3.4.1 spanning tree protocol (STP)

Leetcode 729. My schedule I (awesome, solved)

记一次 .NET 某物管后台服务 卡死分析

Learning notes - data set generation

Glide缓存机制

matlab习题 —— 创建 50 行 50 列全零矩阵、全 1 矩阵、单位矩阵、对角矩阵,输出矩阵第135号元素。

Prevent being rectified after 00? I. The company's recruitment requires that employees cannot sue the company
![Leetcode 522 longest special sequence ii[enumeration double pointer] leetcode path of heroding](/img/b1/80a51b403dfb0611b87ef9aa3215af.png)
Leetcode 522 longest special sequence ii[enumeration double pointer] leetcode path of heroding

嵌入式软件架构设计-模块化

【TcaplusDB知识库】TcaplusDB-tcaplusadmin工具介绍
随机推荐
[tcapulusdb knowledge base] Introduction to new models of tcapulusdb
【值得收藏】Centos7 安装mysql完整操作命令
中科院微生物所招聘青年PI 20比特,2百萬安家費,千萬啟動經費(長期有效)
deep learning statistical arbitrage
ECMAScript 6(es6)
Informatics Olympiad all in one 1332: [example 2-1] weekend dance
Eureka核心源码解析
21: Chapter 3: develop pass service: 4: further improve [send SMS, interface]; (in [send SMS, interface], call Alibaba cloud SMS service and redis service; a design idea: basecontroller;)
防止被00后整顿?一公司招聘要求员工不能起诉公司
杰理之一直喂狗会频繁开关中断导致定时器【篇】
matlab习题 —— 创建 50 行 50 列全零矩阵、全 1 矩阵、单位矩阵、对角矩阵,输出矩阵第135号元素。
数据库之元数据
一篇抄十篇,CVPR Oral被指大量抄袭
Metadata of database
【TcaplusDB知识库】Tmonitor单机安装指引介绍(二)
[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (I)
Co jump
[tcapulusdb knowledge base] tcapulusdb tmonitor module architecture introduction
【Methodot 专题】什么样的低代码平台更适合开发者?
[tcapulusdb knowledge base] Introduction to tmonitor background one click installation (I)