当前位置:网站首页>Introduction to flexible array
Introduction to flexible array
2022-06-25 14:58:00 【--988】
Flexible array :
C99 in , The last element in the structure is allowed to be an array of unknown size , This is called a flexible array member .
Some compilers will report errors and cannot compile , This can be written in the form left .
struct S struct S
{ {
int n; int n;
int arr[]; int arr[0];// Unknown size , It can be resized
}; };Here is an example of using a flexible array :
struct S
{
int n;
int arr[];
};
int main()
{
struct S *ps=(struct*)malloc(sizeof(struct S)+5*sizeof(int));
ps->n=100;
int i=0;
for(i=0;i<5;i++)
{
ps->arr[i]=i;
}
struct S *ptr=realloc(ps,44);
if(ptr!=NULL)
{
ps=ptr;
}
for(i=5;i<10;i++)
{
ps->arr[i]=i;
}
for(i=0;i<10;i++)
{
pintf("%d ',ps->arr[i]);
}
free(ps)
ps=NULL;
return 0;
}
Here is an example of using a pointer to resize :
struct S
{int n;
int *arr;
};
int mian()
{
struct S*ps=(struct S*)malloc(sizeof(struct S));
ps->arr=malloc(5*sizeof(int));
int i=0;
for(i=0;i<5;i++)
{
ps->arr[i]=i;
}
int *ptr=realloc(ps->arr,10*sizeof(int));
if(ptr!=NULL)
{
ps=ptr;
}
for(i=5;i<10;i++)
{
ps->arr[i]=i;
}
for(i=0;i<10;i++)
{
printf("%d ",ps->arr[i]);
}
free(ps->arr);
ps->arr=NULL;
free(ps);
ps=NULL;
return 0;
}

The characteristics of flexible arrays :
- A flexible array member in a structure must be preceded by at least one other member
- 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 for dynamic memory allocation , And the allocated memory should be smaller than the size of the structure , To fit the expected size of the flexible array .
advantage :
① Convenient memory release
② Improve access speed , Reduce memory fragmentation
Video for detailed introduction https://www.bilibili.com/video/BV1454y1v7wA?p=63&share_source=copy_web
边栏推荐
- Thymeleaf Usage Summary
- Native JS obtains form data and highlights and beautifies JSON output display
- 电源自动测试系统NSAT-8000,精准高速可靠的电源测试设备
- QT opens the print dialog box in a text editor
- 15 -- k points closest to the origin
- One question per day,
- Add the resources directory under test in idea
- High precision addition
- QT file reading -qfile
- 分饼干问题
猜你喜欢

如何裁剪动图大小?试试这个在线照片裁剪工具

How to view the Chrome browser plug-in location

使用sphinx根据py源文件自动生成API文档

【中国海洋大学】考研初试复试资料分享

ffmpeg protocol concat 进行ts流合并视频的时间戳计算及其音画同步方式一点浅析

‘make_ unique’ is not a member of ‘std’

How to crop GIF dynamic graph? Take this picture online clipping tool

JS select all exercise

QQ情话糖果情话内容获取并保存

Add the resources directory under test in idea
随机推荐
Ubuntu 20.04 installing mysql8.0 and modifying the MySQL password
C language LNK2019 unresolved external symbols_ Main error
Design and implementation of thread pool
JS select all exercise
New good friend Pinia, leading the new era of state management
Jaspersoft studio installation
p1408
(translation) json-rpc 2.0 specification (Chinese version)
Position (5 ways)
NBD Network Block Device
重磅!国产 IDE 发布,由阿里研发,完全开源!(高性能+高定制性)
High precision addition
[Ocean University of China] information sharing for the first and second examinations of postgraduate entrance examination
【HBZ分享】LockSupport的使用
Learning notes on February 8, 2022 (C language)
QT pop up open file dialog box QFileDialog
HMS Core机器学习服务实现同声传译,支持中英文互译和多种音色语音播报
Luogu p5707 [deep foundation 2. example 12] late for school
JS to add elements to the header, or tail of an array
Is it normal to dig for money? Is it safe to open a stock account?