What happens when a variable goes out of scope?

后端 未结 3 1686
眼角桃花
眼角桃花 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: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."

提交回复
热议问题