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:
- if possible, set the data in mystruct to zero at compilation (e.g. for static variables, as tristopia and Oli Charlesworth remarked in the comments)
- or if not (e.g. auto variables), to generate the assembly code that will set the data to zero when the variable is initialized (which is better than calling a function to do that).
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 } ;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…