在C中初始化int数组而不赋值

时间:2022-11-26 20:14:24

I'm working through K&R exercise 1-13, and I forgot to set the elements in my array to 0. To my surprise, the last value that I got when printing the array was 32767; subsequent tests have different element values for the array, some different, and some not.

我正在做K&R练习1-13,忘记将数组中的元素设置为0。令我惊讶的是,我在打印数组时得到的最后一个值是32767;后续测试对数组有不同的元素值,有些不同,有些不同。

I'd like to know why this is happening. If it's highly complex, then what's going on in simple terms?

我想知道为什么会这样。如果它非常复杂,那么用简单的术语怎么说呢?

#include <stdio.h>

#define IN 1  /* inside a word */
#define OUT 0 /* outside a word */

/* print the length of words as input to a histogram with horizontal bars */
int main() {
  int c, i;
  int state = OUT;
  int accum = 0;
  int nchar[10];

  while ((c = getchar()) != EOF) {
    if (c != ' ' && c != '\n' && c != '\t') {
      state = IN;
      ++accum;
    }
    else {
      state = OUT;
      ++nchar[accum];
      accum = 0;
    }
  }
  for (i = 0; i < 10; ++i)
    printf("%d\n", nchar[i]);
  return 0;
}

Input & Corresponding Output:

对应输入和输出:

hello codes

4195584
0
0
0
4196032
2
4195584
0
-1608045280
32767

3 个解决方案

#1


1  

When the array is created, the compiler claims memory on the stack. Data is written to that memory location, if you are initializing the array or (in general) assigning values to it.

在创建数组时,编译器要求堆栈上的内存。如果正在初始化数组或(通常)为其分配值,则将数据写到该内存位置。

If you do not initialize anything, just memory is claimed, which was already used before for something else. The stack is not zeroed after data gets removed, because it would waste too much processor time and the RAM is getting filled with data again anyway.

如果不初始化任何东西,只声明内存,这在以前已经用于其他东西。在删除数据之后,栈不会被归零,因为这会浪费太多的处理器时间,而且不管怎样,RAM还是会再次被数据填充。

#2


1  

That's simply what happens when you don't initialize your memory. You get whatever was there before your program claimed it...

这就是当你不初始化你的记忆时发生的事情。在你的程序声明之前你得到了任何东西……

#3


1  

Whatever the program that previously ran in your address space put there. So if a program put, say, 77, at address 0xabcd5657, and then you read that address, you'd get 77. This is because C does not zero initialize memory for you, although you can yourself with memset:

无论之前在地址空间中运行的程序是什么。如果一个程序,比如说,77,在地址0xabcd5657,然后你读取那个地址,你会得到77。这是因为C没有为您零初始化内存,尽管您可以使用memset:

memset(nchar, 0, 10);

#1


1  

When the array is created, the compiler claims memory on the stack. Data is written to that memory location, if you are initializing the array or (in general) assigning values to it.

在创建数组时,编译器要求堆栈上的内存。如果正在初始化数组或(通常)为其分配值,则将数据写到该内存位置。

If you do not initialize anything, just memory is claimed, which was already used before for something else. The stack is not zeroed after data gets removed, because it would waste too much processor time and the RAM is getting filled with data again anyway.

如果不初始化任何东西,只声明内存,这在以前已经用于其他东西。在删除数据之后,栈不会被归零,因为这会浪费太多的处理器时间,而且不管怎样,RAM还是会再次被数据填充。

#2


1  

That's simply what happens when you don't initialize your memory. You get whatever was there before your program claimed it...

这就是当你不初始化你的记忆时发生的事情。在你的程序声明之前你得到了任何东西……

#3


1  

Whatever the program that previously ran in your address space put there. So if a program put, say, 77, at address 0xabcd5657, and then you read that address, you'd get 77. This is because C does not zero initialize memory for you, although you can yourself with memset:

无论之前在地址空间中运行的程序是什么。如果一个程序,比如说,77,在地址0xabcd5657,然后你读取那个地址,你会得到77。这是因为C没有为您零初始化内存,尽管您可以使用memset:

memset(nchar, 0, 10);