当前位置:网站首页>C language flexible array
C language flexible array
2022-07-23 10:05:00 【Orange cat】
Flexible array
Concept of flexible array
C99 in , The last element in the structure is allowed to be an array of unknown size , This is called 『 Flexible array 』 member
Flexible array syntax form :
struct S
{
int a;
int arr[0];// there arr[0] Represents an array of unknown size
};
// Or as follows :
struct S
{
int a;
int arr[];
};
The characteristics of flexible arrays
- A flexible array member in a structure must be preceded by at least one other member
struct S
{
int a;
int arr[];
};
A flexible array must have at least two members , Flexible array must be the last member
The first one above may not compile in some compilers , It can be changed to the second method
- sizeof The size of the structure returned does not include the memory of the flexible array .

use sizeof Calculate the size , Flexible arrays are not computed
- 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
The use of flexible arrays
typedef struct s
{
int n;
int arr[];
}s;
int main()
{
s* pd = malloc(sizeof(s) + 40);
pd->n = 100;
int i = 0;
for (i = 0; i < 10; i++)
{
pd->arr[i] = i;
printf("%d ", pd->arr[i]);
}
}
Code results :
- The above code is given in the form of flexible array arr Allocate space and print
to arr Distribute 40 Give byte space
Advantages of flexible arrays
Use pointer mode :
typedef struct s
{
int n;
int* arr;
}s;
int main()
{
s* pd = malloc(sizeof(s));// To members n Open up space
pd->n = 100;
pd->arr = malloc(40);// To members arr Open up space
if (pd->arr == NULL)
{
return 1;
}
// Use
int i = 0;
for (i = 0; i < 10; i++)
{
pd->arr[i] = i;
printf("%d ", pd->arr);
}
// Release
free(pd);// What's released is n member
free(pd->arr);
pd = NULL;
return 0;
}
From the comparison between the above code and the code of flexible array, we can see , The code of flexible array is more convenient , just
malloconce
And the implementation through pointer requiresmallocsecondary , And release it many times , It is troubleAdvantages of flexible arrays :
1. Convenient memory release
Write code and do it twicemallocMemory allocation , And return the whole structure , It's going onfreeWhen releasing , You may forget to release other members of the structure
So we should allocate the memory of the structure and its members at one time , Do it oncefreeYou can also free up all the memory
2. Reduce memory fragmentation
Too much use Dynamic memory functions will fragment memory , It's a waste of space , It will reduce the speed of access
边栏推荐
- 实现多层级条件查询(类似京东多层级添加查询)
- 范式及反范式
- 目前都有哪些年利率6%左右的保本理财产品?
- Is the sub database and sub table really suitable for your system? Talk about how to select sub databases, sub tables and newsql
- Burpsuite 插件的学习与使用
- Realize multi-level conditional query (similar to JD multi-level add query)
- 567. 字符串的排列
- 面试官:解释一下ThreadLocal 核心原理
- 十年磨一剑,云原生分布式数据库PolarDB-X的核心技术演化
- 本地提权的学习
猜你喜欢
随机推荐
中小企业的福音来咯!JNPF渐火,助力业务数字化升级
转行软件测试薪资10K | 手中有粮心中有底,在任何时候都是真理
Qt::WA_TransparentForMouseEvents 了解一下
网络通信原理与IP地址的分配原理,网络七层由下往上分别为物理层、数据链路层、网络层、传输层、会话层、表示层和应用层
PHP RSA generates public key and private key PSA2 encryption and decryption
BGP反射器,联邦,选路规则
技术分享 | 大事务阻塞 show master status
实现方法pathListToMap,能够将输入的pathList转化为具有层级结构的map类型
How to learn SCM based on zero?
【VSCODE】当前工作目录非当前文件夹/pathlib打印cwd路径错误
完成端口的几个重要问题
隐藏网站服务器响应头中 PHP 版本信息
中信期货是正规的期货公司吗,开户是否安全?
枚举类的使用和实现
软件质量管理实践全面总结
十年磨一剑,云原生分布式数据库PolarDB-X的核心技术演化
PHP script paging TXT content case
567. 字符串的排列
The method of page Jump in PHP
七大排序--万字详解







