I do a lot of Win32 programming in C++ and many Win32 structures have a ‘size’ (often called cbSize or length) member as the first element which needs to be set before the relevant API call can be made. For example:
WINDOWPLACEMENT wp;
wp.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(hWnd, &wp);
Now, I think it is good practice to initialize structure members to zero which I can do with:
WINDOWPLACEMENT wp = { };
or
WINDOWPLACEMENT wp = { 0 };
However, what happens to the other members of the struct if I initialize the first member like this:
WINDOWPLACEMENT wp = { sizeof(WINDOWPLACEMENT) };
Are they automatically initialized to zero? Or does it depend on which compiler I’m using and whether it’s a debug build or not?
Yes, they’re automatically initialized to zero.
8.5.1/7: