当前位置:网站首页>[C language] flexible array
[C language] flexible array
2022-06-29 06:38:00 【Sincere George】
Catalog
Comparison with pointer dynamic development
Disadvantages of pointer dynamic development
Flexible array concept :
A flexible array is a special kind of array
It is also the last member of the structure
in other words , It exists in the position of the last member of the structure
characteristic :
1. The size of the flexible array in the structure is unknown , stay sizeof Its size is not calculated in
#include<stdio.h>
struct S
{
int n ;
int arr[0];// perhaps int arr[];
};
main()
{
printf("The size of the structure is %d",sizeof(struct S));
return 0;
}
2. In the structure , If there is a flexible array , It must be preceded by other members , To apply for space ,, Because his size itself is unknown , No size .
3. A structure containing a flexible array depends on malloc Apply for this space dynamically , This reflects the flexibility of flexible arrays , That is, you can control the size , And the allocated space must be larger than the size of other members .
Comparison with pointer dynamic development
In everyday programming , Sometimes it is necessary to store an array with dynamic length in the structure
General practice , Is to define a pointer member in the structure , This pointer member points to the dynamic memory space where the array is located
Pointer dynamic development
#include<stdio.h> struct S { int n; int* arr; }; int main() { struct S* ps = (struct S*)malloc(sizeof(struct S)); ps->n = 100; ps->arr = (int*)malloc(40); free(ps->arr); ps->arr = NULL; free(ps); ps = NULL; return 0; }
The following is implemented through flexible arrays :
Flexible array
#include<stdio.h> struct S { int n; int arr[0];// Flexible array }; int main() { struct S* ps = (struct S*)malloc(sizeof(struct S)+40); // Use // Change the space of the application struct S* tmp= (struct S*)realloc(ps, sizeof(struct S)+80); if (tmp == NULL) { return; } if (tmp != NULL) { ps = tmp; } return 0; }

Disadvantages of pointer dynamic development
1. This method will be developed many times to release the space for dynamic application , It is easy to make mistakes
2. Prone to memory fragmentation
When the pointer dynamically opens up enough , There will be many memory fragments in the middle , Each memory fragment is discontinuous
It will lead to a waste of space

3. Compared to flexible array ,CPU The access speed should be slower
CPU Most of the memory access is after accessing a space , Will access adjacent spaces , Do not access the next contiguous space at one time , This will lead to , A certain degree of deceleration .

Welcome to like collection and attention , If you have any questions, you can ask
边栏推荐
- Meta metauniverse female safety problems occur frequently. How to solve the relevant problems in the metauniverse?
- Share 10 interview questions related to JS promise
- JDBC | Chapter 6: simple use of database connection pool
- Servlet version conflict causes page 404
- Leetcode simple question: judging the color of a grid on a chess board
- 想请教一下,究竟有哪些劵商推荐?在线开户是安全么?
- Aging design guide for applets
- 2022.02.14 - 239. A single element in an ordered array
- Week 10 - task 0- execution process instance resolution of constructors and destructors
- 力扣每日一题-第30天-1523.位1的个数
猜你喜欢

UVM验证平台

Share 10 interview questions related to JS promise

jetson tx2

The echares map is implemented separately by provinces, and the tooltip user-defined prompt box, scattered annotation and scattered illumination are explained in detail

What is MES? What does it do?

Design and practice of kubernetes cluster and application monitoring scheme

Installing modules in pycharm

百度小程序自动提交搜索

Why are keys unordered in golang map

National Defense University project summary
随机推荐
Design of leetcode simple problem goal parser
Failure: unable to log in to "taxpayer equity platform"
Two houses with different colors and the farthest distance
关于DDNS
Baidu applet automatically submits search
层次分析法
Case of single file component files
多线程工具类 CompletableFuture
Go basic data type conversion
Jenkins operation Chapter 6 mail server sending build results
ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience
Yyds dry goods inventory meituan's two-sided experience, and finally there was a surprise?
Unity AR Shadow 阴影
Small program large screen adaptation Guide
Design and practice of kubernetes cluster and application monitoring scheme
How does MySQL implement distributed locks?
SCM engineering experience - time slice
Analysis on the wave of learning robot education for children
Week 12 - task 2- shoulder to shoulder cadres
[MySQL technology topic] technical analysis and guide for analyzing the high availability architecture of MySQL
