当前位置:网站首页>Structure completion (flexible array)
Structure completion (flexible array)
2022-07-21 02:35:00 【A collection of old dreams 186】
author : A collection of old dreams 186
special column :C Language programming ---- Little bit's Growth Diary
Related links :C Detailed explanation of language custom types : Structure
Daily inspirational :
Some people , Only do two things in a lifetime , Refuse to accept , strive for , So it's getting better and better . There are also people. , Only do two things in a lifetime , wait for , regret , So the more mixed, the worse .
Preface :
This article is also the final chapter of the structure , Xiaobian takes you to know the flexible array in the structure .
Catalog
Two . The characteristics of flexible arrays
3、 ... and . The use of flexible arrays
Four . The advantages of flexible arrays
One . Definition :
Maybe you've never heard of Flexible array (flexible array) The concept , But it does exist .C99 in , The last element in the structure is allowed to be an array of unknown size , This is called 『 Flexible array 』 member .
for example :
typedef struct st_type { int i; int a[0];// Flexible array members }type_a;
Some compilers will report errors and cannot compile. They can be changed to :
typedef struct st_type { int i; int a[];// Flexible array members }type_a;
Two . The characteristics of flexible arrays
1. Flexible array members in structure Must be preceded by at least one other member .2.sizeof The size of the structure returned does not include the memory of the flexible array .3. 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 Small , To fit the expected size of the flexible array .
for example :
#include<stdio.h> #include<stdlib.h> typedef struct st_type { int i; int a[0];// Flexible array members }type_a; int main() { printf("%d\n", sizeof(type_a));// The output is 4 }
3、 ... and . The use of flexible arrays
#include<stdio.h> #include<stdlib.h> struct pep { int n; int a[]; }; int main() { struct pep* ps=(struct peo*)malloc(sizeof(struct pep)+40); if (ps == NULL) { return 1; } ps->n = 10; for (int i = 0; i < 10; i++) { ps->a[i] = i; } for (int i = 0; i < 10; i++) { printf("%d",(ps->a[i])); printf("\n"); } free(ps); return 0; }
It is equivalent to applying space for integer and flexible arrays in the heap .
Four . The advantages of flexible arrays
We can compare the two codes
Code 1:
#include<stdio.h> #include<stdlib.h> struct pep { int n; int a[]; }; int main() { struct pep* ps=(struct peo*)malloc(sizeof(struct pep)+40); if (ps == NULL) { return 1; } ps->n = 10; for (int i = 0; i < 10; i++) { ps->a[i] = i; } for (int i = 0; i < 10; i++) { printf("%d",(ps->a[i])); printf("\n"); } free(ps); return 0; }
Code 2:
#include<stdio.h> #include<stdlib.h> struct pep { int n; int *a; }; int main() { struct pep* ps = (struct pep*)malloc(sizeof(struct pep) + 40); if (ps == NULL) { return 1; } ps->n = 10; ps->a = (int*)malloc(40); if (ps->a == NULL) { return 1; } for (int i = 0; i < 10; i++) { ps->a[i] = i; } for (int i = 0; i < 10; i++) { printf("%d", (ps->a[i])); printf("\n"); } free(ps->a); free(ps); ps = NULL; return 0; }
Above Code 1 and Code 2 It can do the same thing , however Method 1 There are two benefits to the implementation of
The first advantage is : Convenient memory releaseIf our code is in a function for others , You do a secondary memory allocation in it , And return the whole structure touser . The user calls free You can release the structure , But the user doesn't know that the members in the structure also need free, So youYou can't expect users to find out . therefore , If we allocate the memory of the structure and the memory of its members at one time了 , And return a structure pointer to the user , The user does it once free You can also free up all the memory .
The second advantage is : This is good for access speed .Continuous memory is good for improving access speed , Also beneficial to reduce Inside save broken slice .( Actually , Personally, I don't think it's much higher , AnywayYou can't run. You have to add offsets to address )
Conclusion :
Everyone's growth is ability and what he wants , Continuous matching process , When your talent doesn't match your desire , You should calm down and study , If Xiaobian's summary can help you , I hope you guys will pay more attention for three consecutive days , Your support is the biggest motivation for Xiaobian's creation .
边栏推荐
- Explain fcos: full revolutionary one stage object detection in detail
- Get the way to optimize the one-stop worktable of customer service
- This beta version of Typora is expired,please download and install a newer version.
- Relationship between accuracy, recall and confidence
- Date从对象中获取工作日的名称
- Functions and advantages of interactive design of AR digital exhibition hall
- jdbc error code
- 何为整型提升(实例)
- Several common models of convolutional neural network CNN
- Exploration and practice of dewu app data simulation platform
猜你喜欢
Explain IOU, giou, Diou, ciou, eiou and Diou NMS in detail
swift 使用SMAMB2包,重新打包的心酸历程
容量调度绝对值配置队列使用与避坑
typora测试版过期无法正常使用
The first sentence of runaway it: a new colleague
The beta version of typera is expired and cannot be used normally
开发者必读:2022年移动应用运营增长洞察白皮书
DiFi: A Go-as-You-Pay Wi-Fi Access System 精读笔记(三)
Excel宏是什么?Excel宏的使用教程
Apache Flink's yarn session submission process
随机推荐
添加给定的工作日数后计算日期
Real time debugging practice based on attach to process
全栈代码测试覆盖率及用例发现系统的建设和实践
Difference between BN and LN
Please stop using simpledateformat to format time. Datetimeformatter is better!
Kingdee's "answer sheet" forecast in the first half of the year: key customer strategy continued to break through, and arr increased by more than 45% year-on-year
Advanced one stage of target detection
容量调度绝对值配置队列使用与避坑
Explain fcos: full revolutionary one stage object detection in detail
gerrit系统如何配置访问控制
This beta version of Typora is expired,please download and install a newer version.
日期范围生成器
The difference between break, continue and return
Several common models of convolutional neural network CNN
To get to the bottom: Principle Analysis of Objective-C correlation attribute
有奖调研 | 让虚拟照入现实的完美AR开发平台长什么样?
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
DiFi: A Go-as-You-Pay Wi-Fi Access System 精读笔记(三)
HMS Core音频编辑服务支持7种音频特效,助力一站式音频处理
网络中的参数量(param)和浮点计算量(FLOPs)的计算