当前位置:网站首页>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
边栏推荐
- Is it safe to open an online stock account? Who knows
- What is the difference between escape, encodeuri and encodeuricomponent?
- [try to hack] vulhub shooting range construction
- Output 0 ~ n digits and output multiple times
- 【中国海洋大学】考研初试复试资料分享
- [untitled] PTA check password
- 15 -- k points closest to the origin
- Master XSS completely from 0 to 1
- JS select all exercise
- Uniapp icon configuration
猜你喜欢

Learning notes on February 18, 2022 (C language)

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

How to cut the size of a moving picture? Try this online photo cropping tool

Flexible layout (display:flex;) Attribute details

Character encoding minutes

Uniapp icon configuration

从408改考自主命题,211贵州大学考研改考

Source code analysis of synergetics and ntyco

15 -- k points closest to the origin

New good friend Pinia, leading the new era of state management
随机推荐
Common classes in QT
One question per day, a classic simulation question
One question per day,
Vs2019 scanf error
15 -- k points closest to the origin
多张动图怎样合成一张gif?仅需三步快速生成gif动画图片
QT pop up open file dialog box QFileDialog
In 2022, the score line of Guangdong college entrance examination was released, and several families were happy and several worried
【Try to Hack】vulnhub DC1
Native JS obtains form data and highlights and beautifies JSON output display
HMS core machine learning service realizes simultaneous interpretation, supports Chinese-English translation and multiple voice broadcast
Daily question, Caesar code,
网上股票开户安不安全?有谁知道呢
Uniapp cloud packaging app
Basic usage of markdown (plain text and grammar)
Two advanced playing methods of QT signal and slot
How to cut the size of a moving picture? Try this online photo cropping tool
Function of getinstance() method
C language LNK2019 unresolved external symbols_ Main error
About the problem of kicad stuck in win10 version, version 6 x