Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an empty initializer list valid C code?

It is common to use {0} to initialize a struct or an array but consider the case when the first field isn't a scalar type. If the first field of struct Person is another struct or array, then this line will result in an error (error: missing braces around initializer).

struct Person person = {0};

At least GCC allows me to use an empty initializer list to accomplish the same thing

struct Person person = {};

But is this valid C code?

Also: Is this line guaranteed to give the same behavior, i.e. a zero-initialized struct?

struct Person person;
like image 929
Marcus Ahlberg Avatar asked Jul 11 '13 09:07

Marcus Ahlberg


People also ask

What is an initializer list in C?

The initializer list is used to directly initialize data members of a class. An initializer list starts after the constructor name and its parameters.

What are the advantages of initializer list?

The most common benefit of doing this is improved performance. If the expression whatever is the same type as member variable x_, the result of the whatever expression is constructed directly inside x_ — the compiler does not make a separate copy of the object.

Why do we use initializer list in C++?

Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon. Following is an example that uses the initializer list to initialize x and y of Point class.

What is an initializer in code?

Initialization code (or boot code) takes the processor from the reset state to a state where the operating system can run. It usually configures the memory controller and processor caches and initializes some devices. In a simple system the operating system might be replaced by a simple scheduler or debug monitor.


3 Answers

No, an empty initializer list is not allowed. This can also be shown by GCC when compiling with -std=c99 -pedantic:

a.c:4: warning: ISO C forbids empty initializer braces 

The reason is the way the grammar is defined in §6.7.9 of the 2011 ISO C Standard:

initializer:          assignment-expression          { initializer-list }          { initializer-list , } initializer-list:          designation(opt) initializer          initializer-list , designation(opt) initializer 

According to that definition, an initializer-list must contain at least one initializer.

like image 127
interjay Avatar answered Sep 21 '22 00:09

interjay


According to the C99 standard, array creation with an empty initializer list is forbidden. In a previous answer, you can see that grammar does not describe this case.


But what happens if you declare an array without initialization? Well, it depends on the compiler which you use. Let's take a look at this simple example: int arr[5] = {}.

GCC

By default gcc does not produce any warnings/errors when you try to compile this code. Not even -Wall, but -Wpedantic does.

warning: ISO C forbids empty initializer braces

But anyway gcc fill members of an array with 0's exactly as if you specify it explicitly int arr[5] = {0} see assembly output godbolt.

CLANG

But default not showing warnings about this case, but with option -Wgnu-empty-initializer does:

warning: use of GNU empty initializer extension

Clang generates different assembly code godbolt but behaves the same.

like image 23
funnydman Avatar answered Sep 21 '22 00:09

funnydman


Yes from C23 empty initialization is allowed. If the initializer is the empty initializer, the initial value is the same as the initialization of a static storage duration object.

struct Person person = {}; //Valid C23

If an object is initialized with an empty initializer, then:

— if it has pointer type, it is initialized to a null pointer;

— if it has decimal floating type, it is initialized to (positive or unsigned) zero, and the quantum exponent is implementation-defined166);

— if it has arithmetic type, and it does not have decimal floating type, it is initialized to (positive or unsigned) zero;

— if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

— if it is a union, the first-named member is initialized (recursively) according to these rules, and any padding is initialized to zero bit.

Refernece: ISO/IEC 9899:202x (E)

like image 37
Learner Avatar answered Sep 20 '22 00:09

Learner