C++ structure initialization with all zeros
Solution 1
But my question also is if it initializes zero to all members, does it also apply for complex structure ?
Yes, all members will be initialized, including "complex" member, but might not be initialized to zero, the final effect is determined by their types.
According to your sample code, struct s
is an aggregate type, then aggregate initialization is performed.
(emphasis mine)
If the number of initializer clauses is less than the number of members
and bases (since C++17)
or initializer list is completely empty, the remaining membersand bases (since C++17)
are initializedby their default initializers, if provided in the class definition, and otherwise (since C++14)
by empty lists, in accordance with the usual list-initialization rules (which performs value-initialization for non-class types and non-aggregate classes with default constructors, and aggregate initialization for aggregates).
For this case the member i
and x
of struct s
will be value initialized to zero.
4) otherwise, the object is zero-initialized.
If struct s
has any other members, they'll be initialized (value initialized or aggregate initialized according to their types) by empty lists recursively.
EDIT
For your added sample (struct scomplex
), the member initial
will be value initialized, and the final effect depends on the type s
. And another member is an array, which will be aggregate initialized with empty list, and all the elements of the array will be value initialized; Same as member initial
, the effect depends on the type s
.
Solution 2
Problem
Will this initialize all of the members to 0?
typedef struct s{
int i;
bool x;
};
int main ()
{
s initial = {};
printf("%d %d", initial.i, initial.x);
}
Answer: yes. Proof? Here you can see it become 0.
Better Alternatives?
This is an opinionated section. But In My Opinion (IMO), initializing it with {0}
would be more readable than {}
, as it notifies the user of the 0. It is actually being filled up with 0's.
s initial = {0};
What is this called?
This is called Aggregate Initialization
, as Dieter Lücking defined, or Value Initialization
, as songyuanyao noted. It's basically a form of initialization where you can initialize a struct with values you would like. For example, let's initialize it with the value 1 instead of 0! You would do:
// Example program
#include <stdio.h>
#include <iostream>
typedef struct s{
int i;
bool x;
};
int main ()
{
s initial = {1,1};
printf("%d %d", initial.i, initial.x);
}
You can see this compiled here. As you can see above, I am doing 1,1
which is normal initialization. As opposed to 0 initialization, you can't just initialize all the parts of the struct as easily as you can with 0.
References
what is aggregate initialization
What do the following phrases mean in C++: zero-, default- and value-initialization?
Glossary
Aggregate Initialization :
Aggregate initialization is a form of list-initialization, which initializes aggregates.
Value Initialization:
Initialize values
This is the initialization performed when a variable is constructed with an empty initializer.
Just
Updated on June 04, 2022Comments
-
Just about 2 years
In C++ if I initialize the structure in the form of "= {}", as in example below, does it ensure to assign values zero to all the member of the structure? I understand this seem duplicate question, But my question also is if it initializes zero to all members, does it also apply for complex structure ? Like structure within structure , or for this each member has to be explicitly assigned value zero in the code?.
typedef struct s{ int i; bool x; }; int main () { s initial = {}; printf("%d %d", initial.i, initial.x); }
Edit: To reference complex structure,
typedef struct scomplex{ s initial; s t[5]; }; int main (void) { scomplex sc = {}; printf ("%d %d %d",sc.initial.i, sc.initial.x, sc.t[0].i); }
-
lapk over 7 yearsen.cppreference.com/w/cpp/language/value_initialization This is value intialization since there is no user-defined default constructor.
-
John Zwinck over 7 yearsIf you don't want this to be closed as duplicate, please post some example code which is not a duplicate.
-
M.M over 7 years@lapk this is aggregate initialization, not value initialization
-
lapk over 7 years@M.M I think, it is aggregate initialization for
struct s
, but value initialization for non-class type members ofstruct s
(since there is no default user-defined constructor forstruct s
), and, therefore, they are 0 initialized. Am I wrong? -
Admin over 7 yearsSure, all values will be zero. See (Aggregate Initialization) en.cppreference.com/w/cpp/language/aggregate_initialization
-
-
M.M over 7 yearswhy is it more readable to give a value to one of the members. By your logic
{1}
would notify the user it's being filled up with 1's. -
amanuel2 over 7 years@M.M sorry i meant
1,1
.. Accidents do happen when you type up anwsers you know ;) -
amanuel2 over 7 years@M.M Ok i am done editing question. If you have critics , will love to hear :)
-
Admin over 7 yearsReading and citing the C++ standard is far behind of being trivial: "If the number of initializer clauses is less than the number of members" makes your answer incomplete.
-
Admin over 7 yearsA well formed answer following a formula. Long writing, saying less.
-
songyuanyao over 7 years@DieterLücking Sorry I can't get what you mean, which part is incomplete? "If the number of initializer clauses is less than the number of members" is not the point of the question, it's just quoted (for completeness).
-
amanuel2 over 7 years@DieterLücking Ok , I didn't at first understand what you meant so I asked , and this person responded "I think he's praising the answerer for following a predetermined form for an answer rather than actually providing any content. I might be wrong, though." And I answered I'm not providing content? I did indeed answer his question IMO , not sure about others. Not sure how i can make a predetermined answer when I answered after the question was asked obviously reading the question being asked. Correct me if i am wrong though ;)
-
Ben Jones over 4 yearsI know this is two years later, but I think it should be said: a compiling program is not proof, even if it is a very good indication of expected behavior. (Just because a lot of c++ behavior can be implementation defined or not defined at all)