当前位置:网站首页>Zero length array
Zero length array
2022-07-03 18:35:00 【heater404】
Zero length array
1 Zero length array concept
GCC/GNU In standard C/C++ On the basis of it, it has made practical expansion , Zero length array is one of the well-known extensions . Most of the time , It is used in variable length arrays , Its definition is as follows :
typedef struct
{
uint32_t length;
uint8_t payload[0];// there 0 Long structure provides very good support for variable length structure .
} packet;
First of all, 0 Length array , Also called flexible array , Make an explanation :
- purpose : The length is 0 The main purpose of the array of is to meet the need for variable length structures
- usage : At the end of a structure , Declare a length of 0 Array of , You can make the structure variable in length . For compilers , In this case, the length is 0 The array of does not take up space , Because the array name itself doesn't take up space , It's just an offset , The symbol array name itself represents an immutable address constant .
2 0 The purpose of the length array
Data buffer used in network communication , The buffer contains a len Fields and data Field , Identify the length of the data and the transmitted data respectively , There are several common design ideas :
- Fixed length data buffer , Set a data buffer of sufficient size . But most of the time , Most of the buffer space is wasted .
- Set a pointer to the actual data , Every time I use it , Dynamically open up data buffer space according to the length of data . Need to open up extra space for data fields , When releasing, you also need to explicitly release the space of the data field .
So it's best to use zero length arrays .
3 example
#pragma pack(1)
typedef struct
{
uint32_t age; // 4
gender gen; // 4
char name[32]; // 32
} person;
#pragma pack(1)
typedef struct
{
uint32_t length;
uint8_t payload[0];
} packet;
We want to person Structure as payload.
person *p = new person{18, models::female, "zhuangjuan"};
packet *pkt = (packet *)malloc(sizeof(packet) + sizeof(person));
pkt->length = sizeof(*p);
memcpy(pkt->payload, p, sizeof(*p));
auto pp = (person *)pkt->payload;
std::cout << "pp->age:" << (uint32_t)pp->age << endl;
std::cout << "pp->gen:" << pp->gen << endl;
std::cout << "pp->name:" << pp->name << endl;
delete pkt;
delete p;
边栏推荐
- [combinatorics] generating function (generating function application scenario | using generating function to solve recursive equation)
- Usage of laravel conditional array in
- The second largest gay dating website in the world was exposed, and the status of programmers in 2022
- What is SQL get connection
- 论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding
- How to analyze the rising and falling rules of London gold trend chart
- 多媒体NFT聚合平台OKALEIDO即将上线,全新的NFT时代或将来临
- 企业级自定义表单引擎解决方案(十二)--表单规则引擎2
- On Data Mining
- Line by line explanation of yolox source code of anchor free series network (6) -- mixup data enhancement
猜你喜欢
![网格图中递增路径的数目[dfs逆向路径+记忆dfs]](/img/57/ff494db248171253996dd6c9110715.png)
网格图中递增路径的数目[dfs逆向路径+记忆dfs]

虚拟机和开发板互Ping问题

What problems can cross-border e-commerce sellers solve with multi platform ERP management system

NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线

Redis on local access server

Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)

English语法_名词 - 分类

Naoqi robot summary 27

Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)

Shell script return value with which output
随机推荐
2022-2028 global solid phase extraction column industry research and trend analysis report
[untitled]
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Summary and Reflection on the third week of winter vacation
SQL injection -day16
Redis core technology and practice - learning notes (VI) how to achieve data consistency between master and slave Libraries
How many convolution methods does deep learning have? (including drawings)
圖像24比特深度轉8比特深度
PHP determines which constellation it belongs to today
[combinatorics] exponential generating function (example of exponential generating function solving multiple set arrangement)
[combinatorics] dislocation problem (recursive formula | general term formula | derivation process)*
[combinatorics] generating function (use generating function to solve the number of solutions of indefinite equation)
189. Rotation array
2022-2028 global marking ink industry research and trend analysis report
PHP MySQL inserts data
[linux]centos 7 reports an error when installing MySQL "no package MySQL server available" no package ZABBIX server MySQL available
AcWing 271. Teacher Yang's photographic arrangement [multidimensional DP]
Shell script return value with which output
[tutorial] build your first application on coreos
What is SQL get connection