C++ array[100] = {0} 如何将整个数组设置为 0?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/629017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how does array[100] = {0} set the entire array to 0?
提问by mahesh
How does the compiler fill values in char array[100] = {0};
? What's the magic behind it?
编译器如何填充值char array[100] = {0};
?它背后有什么魔力?
I wanted to know how internally compiler initializes.
我想知道内部编译器如何初始化。
回答by bk1e
It's not magic.
这不是魔法。
The behavior of this code in C is described in section 6.7.8.21 of the C specification (online draft of C spec): for the elements that don't have a specified value, the compiler initializes pointers to NULL and arithmetic types to zero (and recursively applies this to aggregates).
这段代码在 C 中的行为在 C 规范(C 规范在线草案)的第 6.7.8.21 节中描述:对于没有指定值的元素,编译器将指针初始化为 NULL 并将算术类型初始化为零(并递归地将其应用于聚合)。
The behavior of this code in C++ is described in section 8.5.1.7 of the C++ specification (online draft of C++ spec): the compiler aggregate-initializes the elements that don't have a specified value.
此代码在 C++ 中的行为在 C++ 规范(C++ 规范在线草案)的第 8.5.1.7 节中描述:编译器聚合初始化没有指定值的元素。
Also, note that in C++ (but not C), you can use an empty initializer list, causing the compiler to aggregate-initialize all of the elements of the array:
另外,请注意,在 C++(但不是 C)中,您可以使用空的初始化列表,导致编译器聚合初始化数组的所有元素:
char array[100] = {};
As for what sort of code the compiler might generate when you do this, take a look at this question: Strange assembly from array 0-initialization
至于编译器在执行此操作时可能生成什么样的代码,请看一下这个问题:来自数组 0 初始化的奇怪程序集
回答by qrdl
Implementation is up to compiler developers.
实现取决于编译器开发人员。
If your question is "what will happen with such declaration" - compiler will set first array element to the value you've provided (0) and all others will be set to zero because it is a default value for omitted array elements.
如果您的问题是“这样的声明会发生什么” - 编译器会将第一个数组元素设置为您提供的值 (0),而所有其他元素都将设置为零,因为它是省略数组元素的默认值。
回答by lakshmanaraj
If your compiler is GCC you can also use following syntax:
如果您的编译器是 GCC,您还可以使用以下语法:
int array[256] = {[0 ... 255] = 0};
Please look at http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Designated-Inits.html#Designated-Inits, and note that this is a compiler-specificfeature.
请查看 http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Designated-Inits.html#Designated-Inits,并注意这是一个特定于编译器的功能。
回答by lakshmanaraj
It depends where you put this initialisation.
这取决于你把这个初始化放在哪里。
If the array is static as in
如果数组是静态的
char array[100] = {0};
int main(void)
{
...
}
then it is the compiler that reserves the 100 0 bytes in the data segement of the program. In this case you could have omitted the initialiser.
那么是编译器在程序的数据段中保留了100个0字节。在这种情况下,您可以省略初始化程序。
If your array is auto, then it is another story.
如果你的数组是自动的,那就是另一回事了。
int foo(void)
{
char array[100] = {0};
...
}
In this case at every call of the function foo you will have a hidden memset.
在这种情况下,在每次调用函数 foo 时,您都会有一个隐藏的 memset。
The code above is equivalent to
上面的代码等价于
int foo(void)
{
char array[100];
memset(array, 0, sizeof(array));
....
}
and if you omit the initializer your array will contain random data (the data of the stack).
如果您省略初始化程序,您的数组将包含随机数据(堆栈数据)。
If your local array is declared static like in
如果您的本地数组被声明为静态,如
int foo(void)
{
static char array[100] = {0};
...
}
then it is technically the same case as the first one.
那么它在技术上与第一种情况相同。