What happens when a variable goes out of scope?

后端 未结 3 1656
眼角桃花
眼角桃花 2020-11-27 07:42

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)

相关标签:
3条回答
  • 2020-11-27 08:26

    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.

    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);
    

    which means that the output you see has very little to do with what happens to any variables in your 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.

    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);
    }
    

    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.

    0 讨论(0)
  • 2020-11-27 08:39

    The lifetime of an automatic object ends at the end of the block where it is declared.

    Accessing an object outside of its lifetime is undefined behavior in C.

    (C99, 6.2.4p2) "If an object is referred to outside of its lifetime, the behavior is undefined. The value of a pointer becomes indeterminate when the object it points to reaches the end of its lifetime."

    0 讨论(0)
  • 2020-11-27 08:41

    Local variables are allocated on the stack. They are not "freed" in the sense you think about GC languages, or memory allocated on the heap. They simply go out of scope, and for builtin types the code won't do anything - and for objects, the destructor is called.

    Accessing them beyond their scope is Undefined Behaviour. You were just lucky, as no other code has overwritten that memory area...yet.

    0 讨论(0)
提交回复
热议问题