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

边栏推荐
- 【TcaplusDB知识库】Tmonitor后台一键安装介绍(一)
- 【TcaplusDB知识库】TcaplusDB表数据缓写介绍
- VPT模型视频讲解
- 数据库之元数据
- 杰理之睡眠以后定时唤醒系统继续跑不复位【篇】
- Deep learning in finance in cross sectional sectional predictions for random forests
- Glide caching mechanism
- File name setting causes an error to be written to writelines: oserror: [errno 22] invalid argument
- 【TcaplusDB知识库】TcaplusDB分析型文本导出介绍
- Informatics Olympiad all in one 1332: [example 2-1] weekend dance
猜你喜欢

Ci/cd automatic test_ 16 best practices for CI / CD pipeline to accelerate test automation

NAACL 2022 | TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络

Design and Simulation of direct torque control system for induction motor (motion control matlab/simulink)

If you find any loopholes later, don't tell China!

Challenges of machine learning system in production

What basic functions are required for live e-commerce application development? What is the future development prospect?

go-zero微服务实战系列(七、请求量这么高该如何优化)

如何在 Methodot 中部署 JupyterLab?

21:第三章:开发通行证服务:4:进一步完善【发送短信,接口】;(在【发送短信,接口】中,调用阿里云短信服务和redis服务;一种设计思想:BaseController;)

Future & CompletionService
随机推荐
Oracle group statistics query
0基础了解电商系统如何对接支付渠道
NAACL 2022 | TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络
Queue, two-way queue, and its application
【TcaplusDB知识库】TcaplusDB业务数据备份介绍
Glide缓存机制
Istio related information
[tcapulusdb knowledge base] tcapulusdb cluster management introduction
信息学奥赛一本通 1332:【例2-1】周末舞会
机器学习系统在生产中的挑战
Mail system (based on SMTP protocol and POP3 protocol -c language implementation)
[methodot topic] what kind of low code platform is more suitable for developers?
[cloud enjoys freshness] community weekly · vol.68- Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + business realization
【TcaplusDB知识库】Tmonitor系统升级介绍
红包雨: Redis 和 Lua 的奇妙邂逅
Installation manuelle de MySQL par UBUNTU
Working at home is more tiring than going to work at the company| Community essay solicitation
中科院微生物所招聘青年PI 20比特,2百萬安家費,千萬啟動經費(長期有效)
[learn FPGA programming from scratch -47]: Vision - current situation and development trend of the third generation semiconductor technology
If you find any loopholes later, don't tell China!