是memset的(安培; MYSTRUCT,0,sizeof的MYSTRUCT)一样MYSTRUCT = {0};? [英] Is memset(&mystruct, 0, sizeof mystruct) same as mystruct = { 0 };?
问题描述
我被一个数组/结构的默认阅读有关初始化值,并有这样的疑问:
是 memset的(安培; MYSTRUCT,0,sizeof的MYSTRUCT)
相同 MYSTRUCT = {0};
?
如果不是,有什么区别呢?
是memset的(安培; MYSTRUCT,0,sizeof的MYSTRUCT)一样MYSTRUCT = {0}; ?
块引用>没有。
memset的(安培; MYSTRUCT,0,sizeof的MYSTRUCT);
...会告诉编译器调用,我们执行过程中预计将设置函数在MYSTRUCT为零的数据。
MYSTRUCT = {0};
...将设置告诉编译器通过自身的数据设定为零,这意味着它将:
- 如果可能的话,在编译(例如,用于静态变量,在MYSTRUCT数据设置为零的为 tristopia 和奥利奇查尔斯沃思在评论评论)
- 或如果没有的(如自动变量),生成装配code当变量初始化,将数据设置为零(这是比调用一个函数来做到更好)。
请注意,也许的编译器可以优化到memset的编译时指令(想与第二个版本替换第一个版本),但我不会依赖上为
memset的
是运行时库中的函数,而不是一些语言内在的(我不是一个编译器作者/语言的律师,虽然)这是C ++的到来,我自己的观点是,更可以在编译做,更多的编译器知道在编译的时候,执行甚至开始之前,更好的:它使编译器可能优化code和/或生成警告/错误。
在目前的情况下,使用
MYSTRUCT = {0};
符号来初始化结构
始终是安全的比使用memset的,因为它是非常的非常容易编写C说错话了memset的
没有编译器抱怨。下面的例子表明,它很容易比它似乎做code做不同的事情:
//只有1个字节将被设置为0
memset的(安培; MYSTRUCT,0,sizeof的(炭));//可能会溢出数据,可能损坏
//它周围的数据,并且希望崩溃的过程。
memset的(安培; MYSTRUCT,0,sizeof的(myLARGEstruct));//不会将数据257.相反,它会截断
//整数,并设置每个字节为1
memset的(安培; MYSTRUCT,257的sizeof(MYSTRUCT));//将每个字节设置的sizeof(MYSTRUCT)模256的值
memset的(安培; MYSTRUCT,sizeof的(MYSTRUCT),0);// 将工作。总是。
MYSTRUCT = {0};I'm reading about the initialized values by default of an array/struct and have this question:
is
memset(&mystruct, 0, sizeof mystruct)
same asmystruct = { 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:
- 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 astruct
is always safer than using the memset because it is very very easy write the wrong thing in C with amemset
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屋!