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?
= {0}
does zero-initialize all members of a struct, and have believed it for years now, mistakenly, apparently. Also, this rather official-looking documentation I've been using for years (en.cppreference.com/w/cpp/language/zero_initialization) says thatT t = {} ;
andT {} ;
are both zero initializers, when in fact, according to my tests and take-away above, they are NOT. – Vittoriotypedef
on the struct definition since this is C++, even though I originally started this whole struct initialization stuff in C then ported it to C++ slowly while I was testing. – Vittorio= {0}
sets member variables to zero unless they have a default member initializer, as of C++11. – Endanger= {0}
zero-initializes the entire object". That led me to believe for years thatmyStruct data
default-initialized, according to any defaults set in the struct definition, and (perhaps--wasn't sure) zero-initialized just the members that didn't have defaults, andmyStruct data = {0}
zero-initialized all members, period, no exceptions. I was wrong. I'm glad I understand now. – Vittorio= {0}
was really doing at the time either, hence my statement that many veteran developers probably misunderstand the details of what it's doing. I've had other discussions with people since then and not once has someone said what I learned here today. Literally, I just went from confusion to enlightenment. – Vittorio