当前位置:网站首页>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;
边栏推荐
- [Godot] add menu button
- How to expand the capacity of golang slice slice
- Boost. Asio Library
- 2022-2028 global marking ink industry research and trend analysis report
- Redis core technology and practice - learning notes (VI) how to achieve data consistency between master and slave Libraries
- Unsafe类的使用
- Codeforces Round #803 (Div. 2) C. 3SUM Closure
- How to analyze the rising and falling rules of London gold trend chart
- Lesson 13 of the Blue Bridge Cup -- tree array and line segment tree [exercise]
- 041. (2.10) talk about manpower outsourcing
猜你喜欢
Redis core technology and practice - learning notes (VII) sentinel mechanism
PHP MySQL preprocessing statement
Shell script return value with which output
Bidding procurement scheme management of Oracle project management system
Codeforces Round #803 (Div. 2) C. 3SUM Closure
[enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)
Lesson 13 of the Blue Bridge Cup -- tree array and line segment tree [exercise]
Grammaire anglaise Nom - Classification
Win32: dump file analysis of heap corruption
Kratos微服务框架下实现CQRS架构模式
随机推荐
PHP MySQL Update
How to expand the capacity of golang slice slice
Golang string (string) and byte array ([]byte) are converted to each other
[combinatorics] generating function (property summary | important generating function)*
Computer graduation design PHP sports goods online sales system website
Real time split network (continuous update)
Mysql45 lecture learning notes (II)
webcodecs
What kind of experience is it when the Institute earns 20000 yuan a month?
[combinatorics] exponential generating function (proving that the exponential generating function solves the arrangement of multiple sets)
[combinatorics] generating function (example of generating function | calculating generating function with given general term formula | calculating general term formula with given generating function)
企业级自定义表单引擎解决方案(十二)--表单规则引擎2
[tutorial] build your first application on coreos
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
2022-2028 global scar care product industry research and trend analysis report
win32:堆破壞的dump文件分析
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
How do microservices aggregate API documents? This wave of operation is too good
English语法_名词 - 分类
[combinatorics] generating function (generating function application scenario | using generating function to solve recursive equation)