当前位置:网站首页>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;
边栏推荐
- Codeforces Round #803 (Div. 2) C. 3SUM Closure
- Redis core technology and practice - learning notes (11): why not just string
- [combinatorics] generating function (positive integer splitting | unordered | ordered | allowed repetition | not allowed repetition | unordered not repeated splitting | unordered repeated splitting)
- Win32: dump file analysis of heap corruption
- Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)
- Sensor debugging process
- Line by line explanation of yolox source code of anchor free series network (6) -- mixup data enhancement
- Implementation of cqrs architecture mode under Kratos microservice framework
- What kind of experience is it when the Institute earns 20000 yuan a month?
- Setinterval CPU intensive- Is setInterval CPU intensive?
猜你喜欢

The vscode code is automatically modified to a compliance code when it is formatted and saved

How to analyze the rising and falling rules of London gold trend chart

Summary and Reflection on the third week of winter vacation

Module 9 operation

Theoretical description of linear equations and summary of methods for solving linear equations by eigen

English語法_名詞 - 分類

2022-2028 global lithium battery copper foil industry research and trend analysis report

How to quickly view the inheritance methods of existing models in torchvision?

2022-2028 global plasmid DNA cdmo industry research and trend analysis report

Multifunctional web file manager filestash
随机推荐
Okaleido, a multimedia NFT aggregation platform, is about to go online, and a new NFT era may come
Boost.Asio Library
Redis core technology and practice - learning notes (VIII) sentinel cluster: sentinel hung up
CV in transformer learning notes (continuously updated)
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
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
What London Silver Trading software supports multiple languages
Solve the problem of inaccurate network traffic monitored by ZABBIX with SNMP
189. Rotation array
[untitled]
[enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)
Implementation of cqrs architecture mode under Kratos microservice framework
[combinatorics] exponential generating function (example 2 of solving multiple set permutation with exponential generating function)
Install apache+php+mysql+phpmyadmin xampp and its error resolution
[combinatorics] generating function (example of using generating function to solve the number of solutions of indefinite equation)
Recent learning experience
统计图像中各像素值的数量
This diversion
Reappearance of ASPP (atlas spatial pyramid pooling) code
Redis core technology and practice - learning notes (11): why not just string