C 空结构——这是什么意思/做什么? [英] C empty struct -- what does this mean/do?
问题描述
我在我需要使用的设备的头文件中找到了这段代码,虽然我已经使用 C 语言多年,但我从未遇到过:
struct device {};结构 spi_device {结构设备开发;};
它用于:
int spi_write_then_read(struct spi_device *spi,const 无符号字符 *txbuf,无符号 n_tx,无符号字符 *rxbuf, 无符号 n_rx);
还有这里:
struct spi_device *spi = phy->spi;
定义相同的地方.
我不确定这个定义有什么意义.它位于板的 linux 应用程序的头文件中,但对它的使用感到困惑.任何解释,想法?任何人以前都见过这个(我相信你们中的一些人已经:).
谢谢!:bp:
这不是 C,因为 C 结构必须包含至少一个命名成员:
<块引用>(C11,6.7.2.1 结构和联合说明符 p8)如果结构声明列表不直接或通过匿名结构或匿名联合包含任何命名成员,则行为未定义."
但是一个 GNU C 扩展:
<块引用>GCC 允许 C 结构没有成员:
struct 空 {};
结构的大小为零
https://gcc.gnu.org/onlinedocs/gcc/Empty-Structures.html一个>
我不知道你的例子中这个构造的目的是什么,但总的来说我认为它可以用作结构类型的前向声明.请注意,在 C++ 中它是允许的有一个没有成员的班级.
在 Linux 2.4 中,在 Linux 内核 2.4 中的 spin_lock_t
类型别名的定义中(在 include/linux/spinlock.h 中)有一个带有条件编译的空结构类型示例:
#if (DEBUG_SPINLOCKS <1)/* ... */typedef struct {} spinlock_t;#elif (DEBUG_SPINLOCKS <2)/* ... */类型定义结构{易失性无符号长锁;} spinlock_t;#else/* (DEBUG_SPINLOCKS >= 2) *//* ... */类型定义结构{易失性无符号长锁;volatile unsigned int 胡言乱语;const char *module;} spinlock_t;#万一
目的是在DEBUG_SPINLOCKS
DEBUG_SPINLOCKS <;1
.它还允许定义 spinlock_t
类型的虚拟(零大小)对象.
在(最近的)Linux 内核中的另一个例子是在 include/linux/device.h 中与条件编译一起使用的空结构黑客:
struct acpi_dev_node {#ifdef CONFIG_ACPI无效*句柄;#万一};
关于最后一个例子,请参阅与 Greg Kroah-Hartman 的讨论:
https://lkml.org/lkml/2012/11/19/453>
I found this code in a header file for a device that I need to use, and although I've been doing C for years, I've never run into this:
struct device {
};
struct spi_device {
struct device dev;
};
and it used as in:
int spi_write_then_read(struct spi_device *spi,
const unsigned char *txbuf, unsigned n_tx,
unsigned char *rxbuf, unsigned n_rx);
and also here:
struct spi_device *spi = phy->spi;
where it is defined the same.
I'm not sure what the point is with this definition. It is in a header file for a linux application of the board, but am baffled by it use. Any explanations, ideas? Anyone seen this before (I'm sure some of you have :).
Thanks! :bp:
This is not C as C structures have to contain at least one named member:
(C11, 6.7.2.1 Structure and union specifiers p8) "If the struct-declaration-list does not contain any named members, either directly or via an anonymous structure or anonymous union, the behavior is undefined."
but a GNU C extension:
GCC permits a C structure to have no members:
struct empty { };
The structure has size zero
https://gcc.gnu.org/onlinedocs/gcc/Empty-Structures.html
I don't know what is the purpose of this construct in your example but in general I think it may be used as a forward declaration of the structure type. Note that in C++ it is allowed to have a class with no member.
In Linux 2.4 there is an example of an empty structure type with conditional compilation in the definition of spin_lock_t
type alias in Linux kernel 2.4 (in include/linux/spinlock.h):
#if (DEBUG_SPINLOCKS < 1)
/* ... */
typedef struct { } spinlock_t;
#elif (DEBUG_SPINLOCKS < 2)
/* ... */
typedef struct {
volatile unsigned long lock;
} spinlock_t;
#else /* (DEBUG_SPINLOCKS >= 2) */
/* ... */
typedef struct {
volatile unsigned long lock;
volatile unsigned int babble;
const char *module;
} spinlock_t;
#endif
The purpose is to save some space without having to change the functions API in case DEBUG_SPINLOCKS < 1
. It also allows to define dummy (zero-sized) objects of type spinlock_t
.
Another example in the (recent) Linux kernel of an empty structure hack used with conditional compilation in include/linux/device.h:
struct acpi_dev_node {
#ifdef CONFIG_ACPI
void *handle;
#endif
};
See the discussion with Greg Kroah-Hartman for this last example here:
https://lkml.org/lkml/2012/11/19/453
这篇关于C 空结构——这是什么意思/做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!