简体   繁体   中英

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.

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?

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://docs.python.org/library/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.

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.

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). 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.

has this to say about initialization in Section 6.7.8, subclause 10: 在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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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