当前位置:网站首页>How to use flexible arrays?
How to use flexible arrays?
2022-07-06 22:31:00 【It's Beichen not too PI acridine】
How to use flexible arrays ?
First of all, 0 Length array , Also called flexible array Make an explanation :
purpose : The length is 0 The main purpose of the array is to meet the need for variable length structures
usage : At the end of a structure , Declare a length of 0 Array of , You can make the structure variable in length . For compilers , In this case, the length is 0 The array of does not take up space , Because the array name itself doesn't take up space , It's just an offset , The array name itself represents an immutable address constant
C99 in , The last element in the structure is allowed to be an array of unknown size , This is called a flexible array member , But a flexible array member in a structure must be preceded by at least one other member . Flexible array members allow structures to contain an array of variable size .sizeof The size of the structure returned does not include the memory of the flexible array . Structures that contain flexible array members use malloc () Function to dynamically allocate memory , And the allocated memory should be larger than the size of the structure , To fit the expected size of the flexible array .
How to use flexible arrays ? Here's an example :
typedef struct st_type
{
int i;
int a[0];
}type_a;
Some compilers will report errors and cannot compile. They can be changed to :
typedef struct st_type
{
int i;
int a[];
}type_a;
this sample I People Just can With set The righteous One individual can change Long Of junction structure body , use sizeof(type_a) have to To Of only Yes 4 , Just yes sizeof(i)=sizeof(int). that 0 An array of elements doesn't take up space , Then we can do the lengthening operation .
Allocate memory to the structure through the following expression :
type_a *p = (type_a*)malloc(sizeof(type_a)+100*sizeof(int));
So we give the structure pointer p Allocated a block of memory . use p->item[n] You can simply access variable length elements . But then we use sizeof(*p) Test the size of the structure , It's still found that 4. Isn't it weird ? We allocated space to this array ?
When defining this structure , The size of the module has determined the memory size that does not contain flexible arrays . Flexible arrays are just non staff , It does not occupy the compilation of the structure . Just say that when using a flexible array, you need to treat it as a member of the structure , That's it . And white points , Flexible arrays have nothing to do with structures , It's just “ Selling dog meat with sheep's head ” nothing more , Not a full member of the structure .
C89 I don't support this kind of thing ,C99 Add it to the standard as a special case . however ,C99 What is supported is that incomplete type, instead of zero array, It's the same int item[0]; This form is illegal ,C99 The form of support is in the form of int item[]; It's just that some compilers put int item[0]; As a non-standard extension to support , And in C99 There was this nonstandard extension before the release ,C99 After the release , Some compilers combine the two .
Of course , Since the top is used malloc Function allocates memory , Definitely need to use free Function to free memory :
free(p);
Application scenarios : Variable length arrays are often used in network communication to construct indefinite length packets , Don't waste space, waste network traffic .
边栏推荐
- 枚举与#define 宏的区别
- 2022-07-05 stonedb sub query processing parsing time analysis
- 第3章:类的加载过程(类的生命周期)详解
- C# 三种方式实现Socket数据接收
- SQL server generates auto increment sequence number
- SQL Server生成自增序号
- Puppeteer连接已有Chrome浏览器
- Inno setup packaging and signing Guide
- Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
- 2022-07-05 stonedb的子查询处理解析耗时分析
猜你喜欢
Export MySQL table data in pure mode
第3章:类的加载过程(类的生命周期)详解
Memorabilia of domestic database in June 2022 - ink Sky Wheel
(十八)LCD1602实验
Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
Advantages of link local address in IPv6
Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
自定义 swap 函数
Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution
ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
随机推荐
基於 QEMUv8 搭建 OP-TEE 開發環境
3DMAX assign face map
Mysql database basic operations DML
Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
Research and investment strategy report of China's VOCs catalyst industry (2022 Edition)
Is there any requirement for the value after the case keyword?
go多样化定时任务通用实现与封装
做接口测试都测什么?有哪些通用测试点?
新手程序员该不该背代码?
Attack and defense world ditf Misc
hdu 5077 NAND(暴力打表)
Signed and unsigned keywords
How do I write Flask's excellent debug log message to a file in production?
Inno setup packaging and signing Guide
Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
Assembly and interface technology experiment 5-8259 interrupt experiment
Mise en place d'un environnement de développement OP - tee basé sur qemuv8
GD32F4XX串口接收中断和闲时中断配置
Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
Data storage (1)