Local variable still exists after function returns

前端 未结 5 2133
暖寄归人
暖寄归人 2021-01-05 20:00

I thought that once a function returns, all the local variables declared within (barring those with static keyword) are garbage collected. But when I am trying

相关标签:
5条回答
  • 2021-01-05 20:09

    C doesn't support garbage collection as supported by Java. Read more about garbage collection here

    0 讨论(0)
  • 2021-01-05 20:14

    It's undefined behavior, anything can happen, including appearing to work. The memory probably wasn't overwritten yet, but that doesn't mean you have the right to access it. Yet you did! I hope you're happy! :)

    0 讨论(0)
  • 2021-01-05 20:15

    Logically, q ceases to exist when fun exits.

    Physically (for suitably loose definitions of "physical"), the story is a bit more complicated, and depends on the underlying platform. C does not do garbage collection (not that garbage collection applies in this case). That memory cell (virtual or physical) that q occupied still exists and contains whatever value was last written to it. Depending on the architecture / operating system / whatever, that cell may still be accessible by your program, but that's not guaranteed:

    6.2.4 Storage durations of objects

    2 The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. An object exists, has a constant address,33) and retains its last-stored value throughout its lifetime.34) 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 (or just past) reaches the end of its lifetime.
    33) The term ‘‘constant address’’ means that two pointers to the object constructed at possibly different times will compare equal. The address may be different during two different executions of the same program.

    34) In the case of a volatile object, the last store need not be explicit in the program.

    "Undefined behavior" is the C language's way of dealing with problems by not dealing with them. Basically, the implementation is free to handle the situation any way it chooses to, up to ignoring the problem completely and letting the underlying OS kill the program for doing something naughty.

    In your specific case, accessing that memory cell after fun had exited didn't break anything, and it had not yet been overwritten. That behavior is not guaranteed to be repeatable.

    0 讨论(0)
  • 2021-01-05 20:25

    If you really want it to loose the value, perhaps call another function with at least a few lines of code in it, before doing the printf by accessing the location. Most probably your value would be over written by then.

    But again as mentioned already this is undefined behavior. You can never predict when (or if at all) it crashes or changes. But you cannot rely upon it 'changing or remaining the same' and code an application with any of these assumptions.

    What i am trying to illustrate is, when you make another function call after returning from previous one, another activation record is pushed on to the stack, most likely over writing the previous one including the variable whose value you were accessing via pointer.

    No body is actually garbage collecting or doing a say memset 0 once a function and it's data goes out of scope.

    0 讨论(0)
  • 2021-01-05 20:32

    There's no garbage collection in C. Once the scope of a variable cease to exist, accessing it in any means is illegal. What you see is UB(Undefined behaviour).

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