当前位置:网站首页>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;
边栏推荐
- Administrative division code acquisition
- Computer graduation design PHP makeup sales Beauty shopping mall
- [combinatorics] generating function (positive integer splitting | unordered | ordered | allowed repetition | not allowed repetition | unordered not repeated splitting | unordered repeated splitting)
- [Yu Yue education] world reference materials of Microbiology in Shanghai Jiaotong University
- ES7 - Optimization of promise
- [combinatorics] generating function (use generating function to solve the combination number of multiple sets R)
- Recent learning experience
- 虚拟机和开发板互Ping问题
- win32:堆破壞的dump文件分析
- 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
猜你喜欢

Summary and Reflection on the third week of winter vacation

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

Redis core technology and practice - learning notes (VIII) sentinel cluster: sentinel hung up

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

Computer graduation project PHP library book borrowing management system

The second largest gay dating website in the world was exposed, and the status of programmers in 2022

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

Codeforces Round #803 (Div. 2) C. 3SUM Closure

Shell script return value with which output

多媒体NFT聚合平台OKALEIDO即将上线,全新的NFT时代或将来临
随机推荐
[untitled]
Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
199. Right view of binary tree - breadth search
Caddy server agent
Boost.Asio Library
Line by line explanation of yolox source code of anchor free series network (6) -- mixup data enhancement
Module 9 operation
2022-2028 global sepsis treatment drug industry research and trend analysis report
An academic paper sharing and approval system based on PHP for computer graduation design
[combinatorics] generating function (use generating function to solve the combination number of multiple sets R)
A. Odd Selection【BruteForce】
The second largest gay dating website in the world was exposed, and the status of programmers in 2022
Prototype inheritance..
PHP MySQL where clause
CTO and programmer were both sentenced for losing control of the crawler
[Yu Yue education] world reference materials of Microbiology in Shanghai Jiaotong University
Mature port AI ceaspectus leads the world in the application of AI in terminals, CIMC Feitong advanced products go global, smart terminals, intelligent ports, intelligent terminals
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
Ping problem between virtual machine and development board
Sensor debugging process