为什么我们需要初始化数组为0(零)? [英] Why do we need initialize an array to 0 (null)?

查看:1117
本文介绍了为什么我们需要初始化数组为0(零)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现,在C语言,但我不知道其他语言的它需​​要初始化数组0。

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?

在什么情况下,我需要初始化数组为0,在何种情况下我做不?

In what cases do I need to initialize an array to 0 and in what cases do I not?

推荐答案

您这样做是因为该数组将含有垃圾值(pviously存储在该存储单元的值$ P $)的原因。归零是很好的做法,因为零是一个值,不正确使用时,会导致很容易找到和调试(NULL取消引用等等)的问题。在另一方面,垃圾值可能会给你的IM pression你的程序工作,只有把它在code的一些无关的部分崩溃,因为你的垃圾价值的不正确使用破坏的其他存储位置。

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.

这篇关于为什么我们需要初始化数组为0(零)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆