简体   繁体   English

为什么我们需要将数组初始化为0(空)?

[英]Why do we need initialize an array to 0 (null)?

I found that in C language but I don't know other language it need to initialize an array to 0 or not. 我发现使用C语言,但是我不知道其他语言是否需要将数组初始化为0。

for (i = 0; i < MAXSIZE; i++)  {
    a[i] = 0;
}

Is there a reason for doing this? 有这样做的理由吗?

In what cases do I need to initialize an array to 0 and in what cases do I not? 在什么情况下需要将数组初始化为0,在什么情况下不需要?

For a per-language answer, you'll just have to look in that languages documentation. 对于每种语言的答案,您只需查看该语言文档。

ie. 即。 http://php.net/manual/en/language.types.array.php http://php.net/manual/zh/language.types.array.php

http://docs.python.org/library/array.html http://docs.python.org/library/array.html

http://cvs.haskell.org/Hugs/pages/libraries/base/Data-Array.html http://cvs.haskell.org/Hugs/pages/libraries/base/Data-Array.html

Historically we would initialize an array to 0 to prevent bad data (remnants of other memory usage) from being in the array. 从历史上看,我们会将数组初始化为0,以防止不良数据(其他内存使用量的剩余部分)进入数组。

You need to initialize it for the same reasons you would initialize any other variable, whether it's an array or not. 出于与初始化其他任何变量相同的原因,无论是否为数组,都需要对其进行初始化。 If you are later going to assume something about the contents of that array, you had better know what those contents are. 如果以后要假设有关该数组的内容,则最好知道这些内容是什么。 Whether you set it to zero or some other value is dependent only on your particular program's requirements. 将其设置为零还是其他值仅取决于特定程序的要求。

If you are going to use values from the array right after it is created, you want those values to have something. 如果要在创建数组后立即使用数组中的值,则希望这些值具有某种含义。 In the absence of further information, 0 is a good starting value. 在没有更多信息的情况下, 0是一个很好的起始值。

If you are going to use the array only to store values (at the beginning) and you don't really care what's in it, don't initialize. 如果您打算仅使用数组来存储值(在开始时),并且您实际上并不在意数组中的内容,请不要初始化。 You save a chance to introduce a bug (and a few nanoseconds off the runtime). 您可以节省引入错误的机会(并且可以减少运行时间几纳秒)。

#include <stdio.h>
int main(void) {
    int arr[256];
    int ch;

    for (int k=0; k<256; k++) arr[k] = 0; /* initialized because we're going
                                           ** to use the values */
    while ((ch = getchar()) != EOF) {
        arr[(unsigned char)ch] += 1;        /* use the previous 0 */
    }
}

or 要么

#include <stdio.h>
int main(void) {
    int arr[256];      /* not initialized */
    for (k = 0; k < 256; k++) {
        arr[k] = getchar(); /* initialization from file */
    }
}

The reason you do this is because the array would contain garbage values (the values previously stored at that memory location). 这样做的原因是因为数组将包含垃圾值(以前存储在该内存位置的值)。 Zeroing out is good practice because zero is a value that, when used incorrectly, causes problems that are easy to find and debug (NULL dereference and such). 归零是一个好习惯,因为零是一个值,如果使用不当,它会导致易于发现和调试的问题(NULL取消引用等)。 On the other hand, garbage values may give you the impression that your program works, only to have it crash in some unrelated part of the code because your incorrect usage of the garbage value clobbered some other memory location. 另一方面,垃圾值给您的印象是您的程序可以运行,只是使它在代码的某些无关部分中崩溃,因为您对垃圾值的不正确使用会破坏其他内存位置。

C99 has this to say about initialization in Section 6.7.8, subclause 10: C99在6.7.8节第10节中对初始化有这样的说法:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. 如果未自动初始化具有自动存储期限的对象,则其值不确定。 If an object that has static storage duration is not initialized explicitly, then: 如果未明确初始化具有静态存储持续时间的对象,则:

if it has pointer type, it is initialized to a null pointer; 如果具有指针类型,则将其初始化为空指针;

if it has arithmetic type, it is initialized to (positive or unsigned) zero; 如果具有算术类型,则将其初始化为(正数或无符号)零;

if it is an aggregate, every member is initialized (recursively) according to these rules; 如果是聚合,则根据这些规则(递归)初始化每个成员;

if it is a union, the first named member is initialized (recursively) according to these rules. 如果是联合,则根据这些规则初始化(递归)第一个命名成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM