Handmade Hero»Forums»Code
22 posts
None
C struct initialization to 0 with "= {};"
A word of advice (took me a bit of time to figure what was going on).

By accident I had defined a struct with some default value for one of the members, something like:

struct TestStruct
{
uint64 i;
uint64 j = 0;
};

Then, when doing this in the code

TestStruct test = {};

I noticed that "test.i" would be some non-zero garbage value.
Patrick Lahey
67 posts
C struct initialization to 0 with "= {};"
This is one of the downsides of using a C++ compiler but pretending it is C. As soon as you add a member function or member initializer you no longer have a C struct (colloquially known as a POD or "Plain Old Data" but technically known as an "aggregate") since that is illegal in C and C++ rules apply.
54 posts
C struct initialization to 0 with "= {};"
And C++ rules applying means the compiler will generate a constructor and initialize that value to zero for you. That's why you will have a garbage value in the unitialized value.