C语言零长度数组
2013-11-15 18:22
141 查看
零长度数组在ANSI C 和C++中是不允许的,在GNU C中是可以合法使用的。看urb的结构体可以看到:
对于usb同步传输分包的问题,在一个urb的传输中可以使用零长度数组灵活地进行分包处理。
struct urb { /* private: usb core and host controller only fields in the urb */ struct kref kref; /* reference count of the URB */ void *hcpriv; /* private data for host controller */ atomic_t use_count; /* concurrent submissions counter */ atomic_t reject; /* submissions will fail */ int unlinked; /* unlink error code */ /* public: documented fields in the urb that can be used by drivers */ struct list_head urb_list; /* list head for use by the urb's * current owner */ struct list_head anchor_list; /* the URB may be anchored */ struct usb_anchor *anchor; struct usb_device *dev; /* (in) pointer to associated device */ struct usb_host_endpoint *ep; /* (internal) pointer to endpoint */ unsigned int pipe; /* (in) pipe information */ unsigned int stream_id; /* (in) stream ID */ int status; /* (return) non-ISO status */ unsigned int transfer_flags; /* (in) URB_SHORT_NOT_OK | ...*/ void *transfer_buffer; /* (in) associated data buffer */ dma_addr_t transfer_dma; /* (in) dma addr for transfer_buffer */ struct scatterlist *sg; /* (in) scatter gather buffer list */ int num_sgs; /* (in) number of entries in the sg list */ u32 transfer_buffer_length; /* (in) data buffer length */ u32 actual_length; /* (return) actual transfer length */ unsigned char *setup_packet; /* (in) setup packet (control only) */ dma_addr_t setup_dma; /* (in) dma addr for setup_packet */ int start_frame; /* (modify) start frame (ISO) */ int number_of_packets; /* (in) number of ISO packets */ int interval; /* (modify) transfer interval * (INT/ISO) */ int error_count; /* (return) number of ISO errors */ void *context; /* (in) context for completion */ usb_complete_t complete; /* (in) completion routine */ struct usb_iso_packet_descriptor iso_frame_desc[0]; /* (in) ISO ONLY */ };首先零长度的数组是不占用内存的,在为一般结构体分配内存时首先为结构体分配一块内存空间,其次为结构体中的成员变量分配内存空间,这样两次分配的内存空间是不连续的,需要分别对其进行管理。当使用零长度数组时,则是采用一次分配的原则,一次性将所需的内存分配给它,并且内存是连续的。通过申请一段结构体长度加可变长度的内存空间,零长度数组就可以指向可变长度的内存空间,并可采用数组的方式对齐访问。
对于usb同步传输分包的问题,在一个urb的传输中可以使用零长度数组灵活地进行分包处理。
相关文章推荐
- C语言长度为0的数组
- C语言中计算一个数组长度
- c语言怎么获取数组的长度
- 【c语言】数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字
- C语言中可以求任意一个int型数组的长度吗?
- C语言中用于计算数组长度的函数 “strlen() ”。
- C语言数组长度极限
- C语言 可变长度的数组 -2
- C语言中长度为0的数组
- 读陈浩的《C语言结构体里的成员数组和指针》总结,零长度数组
- C语言返璞归真之修改const常量和使用变量定义数组长度
- c语言中数组长度的计算
- 长度为0的数组——C语言的非标准用法之一
- C语言中用于计算数组长度的函数 “strlen() ”。
- 关于变长数组的一点小想法-C语言定义数组但是数组长度不确定怎么办
- C语言中 数组作为函数形参传递相当于指针,在函数中不能得到数组长度,只能得到指针长度4
- C语言读取数组长度
- [C语言]不创建累加器sum变量,如何求一个数组的长度?
- C语言获取数组长度示例
- c语言中求数组长度的