当变量超出范围时,会发生什么? [英] What happens when a variable goes out of scope?

查看:267
本文介绍了当变量超出范围时,会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大多数托管语言(也就是,用GC的那些),局部变量超出范围无法访问,并有较高的GC-优先级(因此,他们将首先被释放)。

In most managed languages (that is, the ones with a GC), local variables that go out of scope are inaccessible and have a higher GC-priority (hence, they'll be freed first).

现在,C是不是一个托管语言,会发生什么情况是走出去的范围在这里变量?

Now, C is not a managed language, what happens to variables that go out of scope here?

我创建了一个在C小测试用例:

I created a small test-case in C:

#include <stdio.h>
int main(void){
    int *ptr;

    {
        // New scope
        int tmp = 17;
        ptr = &tmp; // Just to see if the memory is cleared
    }

    //printf("tmp = %d", tmp); // Compile-time error (as expected)
    printf("ptr = %d\n", *ptr);

    return 0;
}

我使用GCC 4.7.3编译和上述打印程序 17 ,为什么呢?而当/下将局部变量被释放什么情况?

I'm using GCC 4.7.3 to compile and the program above prints 17, why? And when/under what circumstances will the local variables be freed?

推荐答案

您code样品的实际行为是由两个主要因素决定:1)行为的未定义的由语言,2)优化的编译器将生成机code,这并不符合实际你的C code。

The actual behavior of your code sample is determined by two primary factors: 1) the behavior is undefined by the language, 2) an optimizing compiler will generate machine code that does not physically match your C code.

例如,尽管这种行为是未定义,GCC可以(而且会)轻松地优化您的code到只有

For example, despite the fact that the behavior is undefined, GCC can (and will) easily optimize your code to a mere

printf("ptr = %d\n", 17);

这意味着你可以看到输出已经很少做什么在你的code发生任何变数。

which means that the output you see has very little to do with what happens to any variables in your code.

如果你希望你的code的行为,以更好地反映什么身体情况,应声明的指针挥发性。该行为仍然是不确定的,但至少它会限制一些优化。

If you want the behavior of your code to better reflect what happens physically, you should declare your pointers volatile. The behavior will still be undefined, but at least it will restrict some optimizations.

现在,作为外出时的范围会发生什么局部变量。没有任何物理反应。典型的实现将在程序堆栈分配足够的空间给所有变量存储在块嵌套在当前函数中的最深层次。这个空间通常是分配在栈中的函数启动一杆,并释放回在函数退出。

Now, as to what happens to local variables when they go out of scope. Nothing physical happens. A typical implementation will allocate enough space in the program stack to store all variables at the deepest level of block nesting in the current function. This space is typically allocated in the stack in one shot at the function startup and released back at the function exit.

这意味着内存以前由 TMP 继续占据在堆栈中,直到函数退出仍然保留。这也意味着,相同的堆栈空间可以(并且会)由具有近似局部性深度的在同级块中相同的水平不同的变量被重用。直到一些兄弟块变量声明其他一些变量覆盖它的空间将举行最后一个变量的值。在你的榜样,没有人会覆盖原来由 TMP 所占用的空间,所以你通常会看到值 17 在完好无损内存。

That means that the memory formerly occupied by tmp continues to remain reserved in the stack until the function exits. That also means that the same stack space can (and will) be reused by different variables having approximately the same level of "locality depth" in sibling blocks. The space will hold the value of the last variable until some other variable declared in some sibling block variable overrides it. In your example nobody overrides the space formerly occupied by tmp, so you will typically see the value 17 survive intact in that memory.

然而,如果你做到这一点。

However, if you do this

int main(void) {
  volatile int *ptr;
  volatile int *ptrd;

  { // Block
    int tmp = 17;
    ptr = &tmp; // Just to see if the memory is cleared
  }

  { // Sibling block
    int d = 5;
    ptrd = &d;
  }

  printf("ptr = %d %d\n", *ptr, *ptrd);
  printf("%p %p\n", ptr, ptrd);
}

您将看到的空间以前由 TMP 占用已重新用于 D 及其前值一直覆盖。这两个指针第二个的printf 通常输出相同的指针值。

you will see that the space formerly occupied by tmp has been reused for d and its former value has been overriden. The second printf will typically output the same pointer value for both pointers.

这篇关于当变量超出范围时,会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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