memset(&mystruct, 0, sizeof mystruct) 和 mystruct = { 0 }; 一样吗? [英] Is memset(&mystruct, 0, sizeof mystruct) same as mystruct = { 0 };?
问题描述
我正在阅读数组/结构的默认初始化值,并有以下问题:
I'm reading about the initialized values by default of an array/struct and have this question:
是 memset(&mystruct, 0, sizeof mystruct)
和 mystruct = { 0 };
一样吗?
如果不是,那有什么区别?
if it's not, what's difference?
推荐答案
是 memset(&mystruct, 0, sizeof mystruct) 与 mystruct = { 0 }; 相同?
is memset(&mystruct, 0, sizeof mystruct) same as mystruct = { 0 }; ?
没有
memset(&mystruct, 0, sizeof mystruct) ;
... 将告诉编译器调用我们期望将执行期间 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:
- 如果可能,编译时将 mystruct 中的数据设置为零(例如对于静态变量,如 tristopia 和 Oli Charlesworth 在评论中评论)
- 或如果不是(例如自动变量),以生成在变量初始化时将数据设置为零的汇编代码(这比调用函数来执行此操作要好).
- 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).
请注意,也许编译器可以将 memset 优化为编译时指令(就像用第二个版本替换第一个版本),但我不会依赖因为 memset
是来自运行时库的函数,而不是某种语言内在的(我不是编译器作者/语言律师,虽然).
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).
来自 C++,我自己的观点是,您在编译时可以做的越多,编译器在编译时知道的越多,甚至在执行开始之前就越好:它使编译器能够优化代码和/或生成警告/错误.
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.
在当前情况下,使用 mystruct = { 0 };
符号来初始化 struct
总是比使用 memset 更安全,因为它非常非常 使用 memset
很容易在 C 中写出错误的东西,而编译器不会抱怨.
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 } ;
这篇关于memset(&mystruct, 0, sizeof mystruct) 和 mystruct = { 0 }; 一样吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!