Null'ing structures
-
In most source code, when initializing structures to zero, you mostly find something like this:
MYSTRUCT mystruct;
ZeroMemory(&mystruct,sizeof(mystruct));I was wondering... what's wrong with a simple
MYSTRUCT mystruct = {0};
:confused:
Cheers,
Marc:beer: Click to see my *real* signature :beer:
-
In most source code, when initializing structures to zero, you mostly find something like this:
MYSTRUCT mystruct;
ZeroMemory(&mystruct,sizeof(mystruct));I was wondering... what's wrong with a simple
MYSTRUCT mystruct = {0};
:confused:
Cheers,
Marc:beer: Click to see my *real* signature :beer:
-
In most source code, when initializing structures to zero, you mostly find something like this:
MYSTRUCT mystruct;
ZeroMemory(&mystruct,sizeof(mystruct));I was wondering... what's wrong with a simple
MYSTRUCT mystruct = {0};
:confused:
Cheers,
Marc:beer: Click to see my *real* signature :beer:
It's all about making readable code for yourself and others, weeks after you write it. = {0} works. But it's not as obvious that the entire structure will be zeroed. ZeroMemory usually gets compiled as memset with 0, but ZeroMemory is much clearer to read. Back in the days when drive space and memory was expensive, it paid to write very small source code. We learned to be VERY thrifty with every byte, at the expense of readability. But the cost of maintenance should not be underestimated. If you can save another programmer just 2 minutes by making the code easier to read and understand, you've justified the use of a few extra bytes of source code. Scot Brennecke Software Developer VC++ MVP
-
It's all about making readable code for yourself and others, weeks after you write it. = {0} works. But it's not as obvious that the entire structure will be zeroed. ZeroMemory usually gets compiled as memset with 0, but ZeroMemory is much clearer to read. Back in the days when drive space and memory was expensive, it paid to write very small source code. We learned to be VERY thrifty with every byte, at the expense of readability. But the cost of maintenance should not be underestimated. If you can save another programmer just 2 minutes by making the code easier to read and understand, you've justified the use of a few extra bytes of source code. Scot Brennecke Software Developer VC++ MVP
I got a bit curious, and fetched the decompiler...
= {0}
is translated to 20 bytes of inline machine code in my test program whereas theZeroMemory
approach is worth 16 bytes of inline machine code which includes a call to a more complex function with several jump instructions. Scot Brennecke wrote: but ZeroMemory is much clearer to read Not in assembly ;PCheers,
Marc:beer: Click to see my *real* signature :beer:
-
I got a bit curious, and fetched the decompiler...
= {0}
is translated to 20 bytes of inline machine code in my test program whereas theZeroMemory
approach is worth 16 bytes of inline machine code which includes a call to a more complex function with several jump instructions. Scot Brennecke wrote: but ZeroMemory is much clearer to read Not in assembly ;PCheers,
Marc:beer: Click to see my *real* signature :beer:
OK, since you want to "fight" about it... ;) = {0} is not strictly equivalent to the ZeroMemory approach. Most significantly, if your struct is declared on the stack, you do not get what you want: "If initializer-list has fewer values than an aggregate type, the remaining members or elements of the aggregate type are initialized to 0 for external and static variables. The initial value of an automatic identifier not explicitly initialized is undefined." Scot Brennecke Software Developer VC++ MVP
-
OK, since you want to "fight" about it... ;) = {0} is not strictly equivalent to the ZeroMemory approach. Most significantly, if your struct is declared on the stack, you do not get what you want: "If initializer-list has fewer values than an aggregate type, the remaining members or elements of the aggregate type are initialized to 0 for external and static variables. The initial value of an automatic identifier not explicitly initialized is undefined." Scot Brennecke Software Developer VC++ MVP
damn.. well ok, you win, you got the docs on your side :-D MSDN wrote: The initial value of an automatic identifier not explicitly initialized is undefined ..if this is true I think my code needs a little rewrite, but I'm quite sure this isn't true (at least for MS Visual C++).
Cheers,
Marc:beer: Click to see my *real* signature :beer: