I'm reading about the initialized values by default of an array/struct and have this question:
is memset(&mystruct, 0, sizeof mystruct)
same as mystruct = { 0 };
?
if it's not, what's difference?
is memset(&mystruct, 0, sizeof mystruct) same as mystruct = { 0 }; ?
No.
memset(&mystruct, 0, sizeof mystruct) ;
... will tell the compiler to call a function that we expect will set during execution the data in mystruct to zero.
mystruct = { 0 };
... will set tell the compiler set by itself the data to zero, which means it will:
Note that perhaps the compiler could optimize the memset into a compile-time instruction (like replacing the first version with the second version), but I wouldn't rely on that as memset
is a function from the runtime library, not some language intrinsic (I'm not a compiler writer/language lawyer, though).
Coming from C++, my own viewpoint is that the more you can do at compilation and the more the compiler knows at compile time, before the execution even starts, the better: It enables the compiler to possibly optimize the code and/or generate warning/errors.
In the current case, using the mystruct = { 0 };
notation to initialize a struct
is always safer than using the memset because it is very very easy write the wrong thing in C with a memset
without the compiler complaining.
The following examples show that it is easy for the code to do something different than it appears to do:
// only the 1st byte will be set to 0 memset(&mystruct, 0, sizeof(char)) ; // will probably overrun the data, possibly corrupting // the data around it, and you hope, crashing the process. memset(&mystruct, 0, sizeof(myLARGEstruct)) ; // will NOT set the data to 257. Instead it will truncate the // integer and set each byte to 1 memset(&mystruct, 257, sizeof(mystruct)) ; // will set each byte to the value of sizeof(mystruct) modulo 256 memset(&mystruct, sizeof(mystruct), 0) ; // will work. Always. mystruct = { 0 } ;
This is a completely pedantic answer, but given that the internal representation of a null pointer is not guaranted to be 0
the behavior of memset
versus brace-initialization would differ (memset
would do the wrong thing). That said, I've never heard of an implementation that took on this liberty to have a non all 0 bit pattern for null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With