当前位置:网站首页>Zero length array in GNU C
Zero length array in GNU C
2022-07-05 08:05:00 【Car chezi】
List of articles
What is a zero length array (Arrays of Length Zero)
If we define a zero length array in the program , You'll find that in addition to GCC compiler , In other compiling environments, it may fail to compile or have warning messages .
Let's take a very simple example :
int buffer[0];
int main(void)
{
printf("%d\n", sizeof(buffer));
return 0;
}
You'll find that , The output is 0
in other words , The length is 0 The array of does not occupy memory space .
Use of zero length arrays
however , Zero length arrays are generally not used alone , It is often used as the last member of the structure , To form a variable length structure . Let's look at an example
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
struct line {
short len;
char contents[0];
};
int main(void)
{
const char *txt = "hello";
int my_len = strlen(txt) + 1;
struct line *my_line =
(struct line *) malloc (sizeof (struct line) + my_len);
my_line->len = my_len;
memcpy(my_line->contents, txt, my_len);
printf("%s\n",my_line->contents);
printf("sizeof struct line = %lu\n", sizeof (struct line));
printf("address struct line = %p\n", my_line);
printf("address of char contents[0] = %p\n", &my_line->contents);
}
The output is :
hello
sizeof struct line = 2
address struct line = 0x8c8010
address of char contents[0] = 0x8c8012
Be careful :
- struct line Its size is 2 Bytes , Description zero length array contents It really doesn't take up space
- contents The starting address of the array is short len Behind ; The reason is that the type of array is char, If change to int , So in short len There is filling behind
- We can use structural members contents Access memory ( Code 17-18 That's ok ).contents It's like a label , Represents an address constant
- malloc Only called once ( The first 15 That's ok )
With this example , The advantage of zero length arrays is , You can give it as needed struct line Allocate space ,
struct line *my_line = (struct line *) malloc (sizeof (struct line) + my_len);
How much do you want , Just add how much .
The difference between zero length array and pointer
Some people say , It can also be achieved with pointers . such as
struct line {
short len;
void *contents;
};
There is a difference :
- void *contents It's a variable , It takes up memory
- In use ,contents Point to another piece of memory , That is to say, it will be called again malloc
- contents Point to memory and struct line The memory occupied is usually discontinuous
therefore , The advantage of zero length array is reflected .
Define what type of zero length array
Some people think that , The zero length array at the end of the structure should be char type , The advantage is to save space , There is no need to fill in front of it . It's not , The specific type depends on the application .
such as Linux There is a structure in the kernel ——USB request block
struct urb {
struct kref kref;
void *hcpriv;
atomic_t use_count;
atomic_t reject;
int unlinked;
...
int error_count;
void *context;
usb_complete_t complete;
struct usb_iso_packet_descriptor iso_frame_desc[0];
};
11: The zero length array it uses is struct usb_iso_packet_descriptor type , instead of char type .
Reference material
The embedded C Language self-cultivation 05: Zero length array - You know
边栏推荐
- Volatile of C language
- Adaptive filter
- Makefile application
- Global and Chinese markets of large aperture scintillators 2022-2028: Research Report on technology, participants, trends, market size and share
- UEFI development learning 2 - running ovmf in QEMU
- MLPerf Training v2.0 榜单发布,在同等GPU配置下百度飞桨性能世界第一
- 找不到实时聊天软件?给你推荐电商企业都在用的!
- Cadence simulation encountered "input.scs": can not open input file change path problem
- Matlab2018b problem solving when installing embedded coder support package for stmicroelectronic
- Random function usage notes
猜你喜欢

Hardware and software solution of FPGA key chattering elimination

Record the visual shock of the Winter Olympics and the introduction of the screen 2
![[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code](/img/58/576b6b77509ed7a9bef138f3899e37.jpg)
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code

Hardware 3 -- function of voltage follower
![C WinForm [view status bar -- statusstrip] - Practice 2](/img/40/63065e6c4dc4e9fcb3e898981f518a.jpg)
C WinForm [view status bar -- statusstrip] - Practice 2

Development tools -- gcc compiler usage

MySQL blind note common functions

UEFI development learning 6 - creation of protocol
![C WinForm [get file path -- traverse folder pictures] - practical exercise 6](/img/8b/1e470de4e4ecd4fd1bb8e5cf23f466.jpg)
C WinForm [get file path -- traverse folder pictures] - practical exercise 6
![Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev](/img/42/d857452ccfeccbbf1ac34f47e47e2e.jpg)
Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev
随机推荐
Record the opening ceremony of Beijing Winter Olympics with display equipment
[professional literacy] specific direction of analog integrated circuits
研究发现,跨境电商客服系统都有这五点功能!
Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
Define in and define out
Global and Chinese market of rammers 2022-2028: Research Report on technology, participants, trends, market size and share
Connection mode - bridge and net
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
Step motor generates S-curve upper computer
Compilation warning solution sorting in Quartus II
Consul安装
如何将EasyCVR平台RTSP接入的设备数据迁移到EasyNVR中?
My-basic application 1: introduction to my-basic parser
Design a clock frequency division circuit that can be switched arbitrarily
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
Embedded composition and route
Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
万字详解八大排序 必读(代码+动图演示)
Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
Fundamentals of C language