Between the
之间的
int array[100][100];
And
和
int array[100][100]={0};
In the first one, when I print all the elements of the array then after 94th row, halfway through it, I start to get the garbage values but all the values before that are 0, whereas for the second one, all the values are 0.
在第一个中,当我打印数组的所有元素,然后在第94行之后,在中间,我开始得到垃圾值,但是之前的所有值都是0,而对于第二个,所有的值都是0。
Does not the first declaration too initialize with a default 0 value, and if it does not, howcome not all of the values in the array are garbage and why only after 94th row the garbage values are appearing?
第一个声明不是也用默认的0值初始化吗?如果不是,那么为什么数组中的所有值不是都是垃圾,为什么只有在第94行之后垃圾值才会出现?
6 个解决方案
#1
1
Does not the first declaration too initialize with a default 0 value?
第一个声明不是用默认的0值初始化吗?
No. Assuming this is a locally scoped variable, your first declaration does not result in any initialization at all.
不。假设这是一个局部作用域的变量,那么第一个声明根本不会导致任何初始化。
How come not all of the values in the array are garbage and why only after 94th row the garbage values are appearing?
为什么不是数组中的所有值都是垃圾,为什么只有在第94行之后垃圾值才会出现?
Uninitialized memory can have any value, including the values of 0
that you are observing.
未初始化的内存可以有任何值,包括您正在观察的0的值。
#2
1
The first style will cause data to be initialized to 0 only if the variable is a global. Local variables that lack explicit initialization will be equal to whatever happened to be in their storage location when the space was assigned to the variable.
只有当变量是全局变量时,第一个样式才会使数据初始化为0。缺少显式初始化的局部变量将等于当空间被分配给变量时在其存储位置发生的任何事情。
#3
1
The first declaration does not guarantee any initialization, in fact i do not think it performs any.
第一个声明并不保证任何初始化,实际上我不认为它执行任何初始化。
The fact you have clear memory until 94th element is because the OS gave you some clean/ yet unused memory.
直到第94个元素之前,您都有清晰的内存,这是因为操作系统提供了一些干净/尚未使用的内存。
If you want to be sure to have clear memory you have to use the second declaration
如果您想确保拥有清晰的内存,您必须使用第二个声明
#4
1
There's no explicit initialization in the first statement. An implicit initialization is possible, if the array is declared in global scope.
第一个语句中没有显式的初始化。如果数组在全局范围中声明,则可以进行隐式初始化。
The reason of "seems to be half initialized" is that a user space program typically starts with zeroed stack / heap for security reasons (not accessing data from other processes containing passwords etc.), but there's no guarantee that a local variable is always cleared. There's rather a guarantee that is it not because of performance reasons. Even in C there can be a lot of activity happening before main is called: crt0 initializes the runtime, copies static data from read_only sections to global variables etc. The result being that a lot of programs heap and stack are no longer intact.
“似乎只初始化了一半”的原因是,出于安全原因,用户空间程序通常从0的堆栈/堆开始(不访问包含密码的其他进程的数据),但是不能保证始终清除本地变量。这里有一个保证,不是因为性能的原因。即使在C中,在调用main之前也会发生很多活动:crt0初始化运行时,将静态数据从read_only部分复制到全局变量等等。
#5
1
int array[100][100];
int数组[100][100];
if this declaration is in the scope of main i.e in Stack Segment then
memeory for this array will be alloacted in stack. Consider if no one used this stack you may get the value zero otherwise it will garbage,its not particular to 94th row elements.
这个数组的memeory将在堆栈中分配。考虑一下,如果没有人使用这个堆栈,您可能会得到值0,否则它将被垃圾化,这不是第94行元素所特有的。
if same declaration is in Global, then all the array elements will be
instialized to Zero.
instialized为零。
int array[100][100]={0};
int数组[100][100]= { 0 };
Irrespective of any segments [Stack/Data(Global,Static)] if any one
variable got intialized in an array,then rest of the array elements will be intialized to Zero.
变量在数组中被初始化,那么数组元素的其余部分将被初始化为零。
#6
0
First of all +1 for the clean and straight forward question. Now to the answer, the local variables if not explicitly initialized, are not initialized implicitly. All the 93 members containing 0 and not some other garbage value is not a defined behavior. You can try that with doing the above piece of code multiple times and you will see different garbage values returning back for the first declaration. For a better understanding, step into the assembly level of the above code and see for yourself what happens internally.
首先,+1表示干净直接的问题。现在的答案是,如果没有显式初始化局部变量,则不会隐式初始化。包含0而不是其他垃圾值的93个成员都不是定义好的行为。您可以尝试多次执行上述代码片段,您将看到不同的垃圾值返回到第一个声明中。为了更好地理解,请进入上述代码的汇编级别,自己看看内部发生了什么。
#1
1
Does not the first declaration too initialize with a default 0 value?
第一个声明不是用默认的0值初始化吗?
No. Assuming this is a locally scoped variable, your first declaration does not result in any initialization at all.
不。假设这是一个局部作用域的变量,那么第一个声明根本不会导致任何初始化。
How come not all of the values in the array are garbage and why only after 94th row the garbage values are appearing?
为什么不是数组中的所有值都是垃圾,为什么只有在第94行之后垃圾值才会出现?
Uninitialized memory can have any value, including the values of 0
that you are observing.
未初始化的内存可以有任何值,包括您正在观察的0的值。
#2
1
The first style will cause data to be initialized to 0 only if the variable is a global. Local variables that lack explicit initialization will be equal to whatever happened to be in their storage location when the space was assigned to the variable.
只有当变量是全局变量时,第一个样式才会使数据初始化为0。缺少显式初始化的局部变量将等于当空间被分配给变量时在其存储位置发生的任何事情。
#3
1
The first declaration does not guarantee any initialization, in fact i do not think it performs any.
第一个声明并不保证任何初始化,实际上我不认为它执行任何初始化。
The fact you have clear memory until 94th element is because the OS gave you some clean/ yet unused memory.
直到第94个元素之前,您都有清晰的内存,这是因为操作系统提供了一些干净/尚未使用的内存。
If you want to be sure to have clear memory you have to use the second declaration
如果您想确保拥有清晰的内存,您必须使用第二个声明
#4
1
There's no explicit initialization in the first statement. An implicit initialization is possible, if the array is declared in global scope.
第一个语句中没有显式的初始化。如果数组在全局范围中声明,则可以进行隐式初始化。
The reason of "seems to be half initialized" is that a user space program typically starts with zeroed stack / heap for security reasons (not accessing data from other processes containing passwords etc.), but there's no guarantee that a local variable is always cleared. There's rather a guarantee that is it not because of performance reasons. Even in C there can be a lot of activity happening before main is called: crt0 initializes the runtime, copies static data from read_only sections to global variables etc. The result being that a lot of programs heap and stack are no longer intact.
“似乎只初始化了一半”的原因是,出于安全原因,用户空间程序通常从0的堆栈/堆开始(不访问包含密码的其他进程的数据),但是不能保证始终清除本地变量。这里有一个保证,不是因为性能的原因。即使在C中,在调用main之前也会发生很多活动:crt0初始化运行时,将静态数据从read_only部分复制到全局变量等等。
#5
1
int array[100][100];
int数组[100][100];
if this declaration is in the scope of main i.e in Stack Segment then
memeory for this array will be alloacted in stack. Consider if no one used this stack you may get the value zero otherwise it will garbage,its not particular to 94th row elements.
这个数组的memeory将在堆栈中分配。考虑一下,如果没有人使用这个堆栈,您可能会得到值0,否则它将被垃圾化,这不是第94行元素所特有的。
if same declaration is in Global, then all the array elements will be
instialized to Zero.
instialized为零。
int array[100][100]={0};
int数组[100][100]= { 0 };
Irrespective of any segments [Stack/Data(Global,Static)] if any one
variable got intialized in an array,then rest of the array elements will be intialized to Zero.
变量在数组中被初始化,那么数组元素的其余部分将被初始化为零。
#6
0
First of all +1 for the clean and straight forward question. Now to the answer, the local variables if not explicitly initialized, are not initialized implicitly. All the 93 members containing 0 and not some other garbage value is not a defined behavior. You can try that with doing the above piece of code multiple times and you will see different garbage values returning back for the first declaration. For a better understanding, step into the assembly level of the above code and see for yourself what happens internally.
首先,+1表示干净直接的问题。现在的答案是,如果没有显式初始化局部变量,则不会隐式初始化。包含0而不是其他垃圾值的93个成员都不是定义好的行为。您可以尝试多次执行上述代码片段,您将看到不同的垃圾值返回到第一个声明中。为了更好地理解,请进入上述代码的汇编级别,自己看看内部发生了什么。