C 中未初始化的变量或数组 [英] Uninitialized variable or array in C

查看:40
本文介绍了C 中未初始化的变量或数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C 中未初始化数组的结果从何而来?它们是随机分配的值还是存储在内存中的只是以前的值?

#include int main (void){整数值[10];整数索引;值[0] = 197;值[2] = -100;值[5] = 350;值[3] = 值[0] + 值[5];值[9] = 值[5]/10;--值[2];for ( index = 0; index <10; index++ )printf ("values[%i] = %i\n", index, values[index]);返回0;}

和输出:

$ ./a.exe值[0] = 197值[1] = 0值[2] = -101值[3] = 547值[4] = 8值[5] = 350值[6] = 51值[7] = 0值[8] = 44045216值[9] = 35

解决方案

章节和诗句:

<块引用>6.2.4 对象的存储期限
...
5 一个对象,其标识符被声明为没有链接且没有存储类说明符 static 具有自动存储持续时间,一些复合文字也是如此.这尝试间接访问具有自动存储持续时间的对象的结果与对象关联的线程以外的线程是实现定义的.

6 对于这种没有变长数组类型的对象,其生命周期延长从进入与其关联的块直到该块的执行结束反正.(进入封闭块或调用函数会挂起,但不会结束,执行当前块.)如果递归进入该块,则该块的新实例每次都会创建对象.对象的初始值是不确定的.如果为对象指定了初始化,每次声明或在块的执行中达到复合文字;否则,该值变为每次到达声明时都不确定.

强调了.

基本上,具有auto 存储持续时间的对象不会隐式初始化为任何特定值 - 它们具有上次写入该特定内存位置的任何值1.您不能依赖该值为 0(或其他任何值).

上面引用中的最后一句话适用于这样的情况:

for (;;){整数 x;do_something_with( x );}

循环的每次迭代都有效地破坏并重新创建了 x,并且您不能依赖在循环的任何迭代中写入 x 的值来传递到下一次迭代.在实践中在类似 x86 的系统上很可能会继续使用,但不要假设到处都是这种情况.

请注意,在调试模式下构建时,实现可能决定用某个已知值初始化 auto 变量.

<小时>

  1. 其中的机制广泛而多样,不值得在这里讨论.

Where do the results of an uninitialized array in C come from? Are they randomly assigned values or it's just previous values that are stored in the memory?

#include <stdio.h>

int main (void)
{
    int values[10];
    int index; 

    values[0] = 197;
    values[2] = -100;
    values[5] = 350;
    values[3] = values[0] + values[5];
    values[9] = values[5] / 10;
    --values[2];

    for ( index = 0; index < 10; index++ )
        printf ("values[%i] = %i\n", index, values[index]);

    return 0; 
}

and the output:

$ ./a.exe
values[0] = 197
values[1] = 0
values[2] = -101
values[3] = 547
values[4] = 8
values[5] = 350
values[6] = 51
values[7] = 0
values[8] = 44045216
values[9] = 35

解决方案

Chapter and verse:

6.2.4 Storage durations of objects
...
5 An object whose identifier is declared with no linkage and without the storage-class specifier static has automatic storage duration, as do some compound literals. The result of attempting to indirectly access an object with automatic storage duration from a thread other than the one with which the object is associated is implementation-defined.

6 For such an object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in any way. (Entering an enclosed block or calling a function suspends, but does not end, execution of the current block.) If the block is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate. If an initialization is specified for the object, it is performed each time the declaration or compound literal is reached in the execution of the block; otherwise, the value becomes indeterminate each time the declaration is reached.

Emphasis added.

Basically, objects with auto storage duration are not implicitly initialized to any particular value - they have the value of whatever was last written to that particular memory location1. You cannot rely on that value being 0 (or anything else).

That last sentence in the quote above applies to situations like this:

for (;;)
{
  int x;
  do_something_with( x );
}

Each iteration of the loop effectively destroys and re-creates x, and you cannot rely on value written to x in any iteration of the loop to be carried over to the next iteration. In practice on x86-like systems it most likely will be carried over, but don't assume that's the case everywhere.

Note that an implementation may decide to initialize auto variable with some known value when building in debug mode or something.


  1. The mechanics of which are wide and varied and not worth going into here.

这篇关于C 中未初始化的变量或数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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