Is it OK to reference an out-of-scope local variable within the same function?

后端 未结 4 899
野性不改
野性不改 2020-12-20 19:25

In this code, I reference the local variable b even though it is out of scope. But I do it from within the same function so it\'s probably still on the stack, r

4条回答
  •  无人及你
    2020-12-20 20:02

    The spec says

    An instance of each object with automatic storage duration (3.7.3) is associated with each entry into its block. Such an object exists and retains its last-stored value during the execution of the block and while the block is suspended (by a call of a function or receipt of a signal).

    b is an object with automatic storage duration. So, when you are outside the object's block, the object does not exist anymore. You can jump before it and continue write to it, I believe, but not if you jump outside its block. That's too much stretching it.

提交回复
热议问题