Local variable still exists after function return

I thought that as soon as the function returns, all local variables declared inside (with the exception of those that have the static ) are garbage collected. But when I try to execute the following code, it still prints the value after the function returns. Can anyone explain why?

 int *fun(); main() { int *p; p = fun(); printf("%d",*p); //shouldn't print 5, for the variable no longer exists at this address } int *fun() { int q; q = 5; return(&q); } 
+4
source share
5 answers

If you really want the value to lose value, you might call another function with at least a few lines of code before doing printf, referring to the location. Most likely, your value will be written by then.

But then again, as already mentioned, this behavior is undefined. You can never predict when (or if at all) it will fail or change. But you cannot rely on the fact that it "changes or remains unchanged" and encodes the application with any of these assumptions.

What I'm trying to illustrate is that when you call the function again after returning from the previous one, another activation record is pushed onto the stack, most likely over the previous record, including the variable whose value you got through the pointer.

No body actually collects garbage or performs the memset 0 function after the function and data are out of scope.

+4
source

This behavior is undefined, anything can happen, including appearing at work. The memory may not have been overwritten yet, but that does not mean that you have the right to access it. But you did! I hope you will be satisfied!:)

+6
source

In C. There is no garbage collection in C. When the scope of a variable ceases to exist, access to it in any way is illegal. You see the behavior of UB (Undefined).

+6
source

C does not support garbage collection supported by Java. Learn more about garbage collection here.

+2
source

Logically, q ceases to exist when fun completes.

In physical terms (for rather weak definitions of β€œphysical”) the story is a little more complicated and depends on the underlying platform. C does not do garbage collection (not that garbage collection applies in this case). This memory cell (virtual or physical) occupied by q still exists and contains any value that was last written. Depending on the architecture / operating system / regardless of whether this cell may be available to your program, but this is not guaranteed:

6.2.4 Duration of storage of objects

2 The lifetime of an object is part of the execution of the program, during which it is guaranteed to be reserved for it. The object exists, has a permanent address, 33) and saves its last stored value throughout its life. 34) If an object is referenced outside its lifetime, the behavior is undefined. The pointer value becomes undefined if the object it points to (or has just passed) reaches the end of its life.
33) The term "permanent address" means that two pointers to an object built at possibly different times will be compared equal. The address may differ with two different executions of the same program.

34) In the case of a volatile object, the last repository does not have to be explicit in the program.

"Undefined behavior" is a C-language language for solving problems not related to them. In principle, the implementation can freely handle the situation at its discretion, up to completely ignoring the problem and allowing the underlying OS to kill the program to commit something naughty.

In your specific case, access to this memory after the fun exit didn’t break anything, and it has not yet been overwritten. This behavior cannot be repeated.

+1
source

Source: https://habr.com/ru/post/1439876/


All Articles