Will C reuse the memory of a local block var once it goes out of scope?

后端 未结 4 798
执笔经年
执笔经年 2021-01-21 21:01

(I believe this question is technically distinct from Can a local variable's memory be accessed outside its scope? because it is C instead of C++.)

I know that in C

4条回答
  •  攒了一身酷
    2021-01-21 21:34

    You cannot safely look at what that pointer points to, once you're outside the block, exactly as you suspect.

    The "memory location" that was used for the value of X, of course, lives on, so something is likely to be there. But accessing it has no guaranteed safe result; you're likely to see garbage, and in extreme cases it might even crash the program. (And for some amount of time, you might find that the original value of X is still present! That's a red herring; it doesn't make it OK to use.)

提交回复
热议问题