Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the size of an empty struct in C?

Tags:

According to me, it is zero but there seems to be bit confusion here

I have tested it with gcc compiler and it gives me zero as output. I know that in C++, size of an empty class is 1. Let me know if I am missing anything here.

like image 901
Vinit Dhatrak Avatar asked Oct 26 '09 18:10

Vinit Dhatrak


People also ask

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.

What is the size of a struct in C?

The size of the entire structure is 8 bytes. On knowing the structured padding, it is easier to redesign or rewrite the structure.

What is an empty struct?

An empty structIt occupies zero bytes of storage. var s struct{} fmt.Println(unsafe.Sizeof(s)) // prints 0. As the empty struct consumes zero bytes, it follows that it needs no padding. Thus a struct comprised of empty structs also consumes no storage.

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.


2 Answers

A struct cannot be empty in C because the syntax forbids it. Furthermore, there is a semantic constraint that makes behavior undefined if a struct has no named member:

struct-or-union-specifier:   struct-or-union identifieropt { struct-declaration-list }   struct-or-union identifier  struct-or-union:   struct   union  struct-declaration-list:   struct-declaration   struct-declaration-list struct-declaration  struct-declaration:   specifier-qualifier-list struct-declarator-list ;  /* type-specifier or qualifier required here! */ specifier-qualifier-list:   type-specifier specifier-qualifier-listopt   type-qualifier specifier-qualifier-listopt  struct-declarator-list:   struct-declarator   struct-declarator-list , struct-declarator  struct-declarator:   declarator   declaratoropt : constant-expression 

If you write

struct identifier { }; 

It will give you a diagnostic message, because you violate syntactic rules. If you write

struct identifier { int : 0; }; 

Then you have a non-empty struct with no named members, thus making behavior undefined, and not requiring a diagnostic:

If the struct-declaration-list contains no named members, the behavior is undefined.

Notice that the following is disallowed because a flexible array member cannot be the first member:

struct identifier { type ident[]; }; 
like image 148
Johannes Schaub - litb Avatar answered Oct 15 '22 12:10

Johannes Schaub - litb


The C grammar doesn't allow the contents of a struct to be empty - there has to be at least an unnamed bitfield or a named member (as far as the grammar is concerned - I'm not sure if a struct that contains only an unnamed bitfield is otherwise valid).

Support for empty structs in C are an extension in GCC.

In C++ and empty struct/class member-specification is explicitly permitted, but the size is defined to be 1 - unless as part of the empty base optimization the compiler is allowed to make an empty base class take no space in the derived class.

like image 21
Michael Burr Avatar answered Oct 15 '22 10:10

Michael Burr