Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C empty struct -- what does this mean/do?

Tags:

c

struct

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:

like image 504
Billy Pilgrim Avatar asked Jul 10 '14 20:07

Billy Pilgrim


People also ask

What does an empty struct mean?

— A structure or union is defined without any named members (including those specified indirectly via anonymous structures and unions) (6.7.

What is an empty struct in C?

Empty struct in C is undefined behaviour (refer C17 spec, section 6.7. 2.1 ): 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.

Can we have empty structure in C?

It's worth noting that empty structs are only somewhat supported in C and disallowed in C99. Empty structs are supported in C++ but different compilers implement them with varying results (for sizeof and struct offsets), especially once you start throwing inheritance into the mix.

Is struct empty go?

Structure is empty. Explanation: In this example, we created a structure named “articles” in which no fields are declared. Inside the main function, we created a variable “x” and used a switch case to access our structure.


2 Answers

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

like image 164
ouah Avatar answered Oct 01 '22 01:10

ouah


This is not standard C.
C11: 6.2.5-20:

— A structure type describes a sequentially allocated nonempty set of member objects (and, in certain circumstances, an incomplete array), each of which has an optionally specified name and possibly distinct type.

J.2 Undefined behavior:

The behavior is undefined in the following circumstances:
....
A structure or union is defined without any named members (including those specified indirectly via anonymous structures and unions) (6.7.2.1).

GCC uses it as an extension (no more detailed is given there about when/where should it be used). Using this in any program will make it compiler specific.

like image 45
haccks Avatar answered Oct 01 '22 01:10

haccks