array [100] = {0}如何将整个数组设置为0? [英] how does array[100] = {0} set the entire array to 0?
问题描述
编译器如何填充 char array [100] = {0};
中的值?
我想知道编译器是如何初始化的。 div>
这不是魔术。
此C代码的行为在C规范的第6.7.8.21节中描述(在线草图C spec ):对于没有指定值的元素,编译器初始化指针NULL和算术类型为零(并递归地应用于聚合)。
这个代码在C ++中的行为在C ++规范的第8.5.1.7节中有描述( C ++规范的在线草案):编译器聚合 - 初始化没有指定值的元素。
此外,请注意,在C ++(但不是C)中,可以使用空的初始化器列表,使编译器聚合初始化数组的所有元素:
char array [100] = {};
至于编译器在这样做时可能会生成什么样的代码,请看看这个问题:从数组0初始化的奇怪汇编
How does the compiler fill values in char array[100] = {0};
? What's the magic behind it?
I wanted to know how internally compiler initializes.
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).
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.
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:
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
这篇关于array [100] = {0}如何将整个数组设置为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!