After doing a ton of testing and writing this answer on how to initialize a struct to zero in C++ (note: the downvote on it was before my total rewrite of it), I can't understand why = {0}
doesn't set all members of the struct to zero!
If you do this:
struct data_t
{
int num1 = 100;
int num2 = -100;
int num3;
int num4 = 150;
};
data_t d3 = {0};
printf("d3.num1 = %i\nd3.num2 = %i\nd3.num3 = %i\nd3.num4 = %i\n\n",
d3.num1, d3.num2, d3.num3, d3.num4);
...the output is:
d3.num1 = 0
d3.num2 = -100
d3.num3 = 0
d3.num4 = 150
...although I expected the output to be this:
d3.num1 = 0
d3.num2 = 0
d3.num3 = 0
d3.num4 = 0
...which means that only the FIRST member was set to zero, and all the rest were set to their defaults.
I was always under the impression that initializing a struct in any of these 3 ways would zero-initialize it, but obviously I'm wrong!
data_t d{}
data_t d = {}
data_t d = {0}
My key takeaway from this answer is therefore this:
The big take-away here is that NONE of these:
data_t d{}
,data_t d = {}
, anddata_t d = {0}
, actually set all members of a struct to zero!
data_t d{}
sets all values to their defaults defined in the struct.data_t d = {}
also sets all values to their defaults.- And
data_t d = {0}
sets only the FIRST value to zero, and all other values to their defaults.
So, why doesn't initializing a C++ struct to = {0}
set all of its members to 0?
Note that my key take-aways above actually contradict this rather official-looking documentation I've been using for years (https://en.cppreference.com/w/cpp/language/zero_initialization), which says that T t = {} ;
and T {} ;
are both zero initializers, when in fact, according to my tests and take-away above, they are NOT.
References:
- How to initialize a struct to 0 in C++
- Update: I was just pointed to this reference too: What does {0} mean when initializing an object?