Q. During the second call to function test()
,
why does the temp
variable retain its content set during the first call to test()
The stack is NOT zero-ed after use.
Please read the legendary "Hotel Keys" answer for a good understanding.
Update : Please note that the detailed explanation below assumes a commonly used memory management system. It contains references to stack and heap. However, the crux of the matter is that access to unintialised local variables will result in undefined/unspecified behaviour. Which means one could read uninitialised local variables as all 0
s or all 1
s or 42
or anything else.
For a moment, ignoring the issues with readability and type safety in the sample program, look at these 2 lines of code in your main()
function:
test(count); <-- Here you set the value of a local variable temp
which does
struct node * temp;
temp = (struct node*)malloc( sizeof( struct node ) );
temp->data = 2016;
printf( "\n%d %u\n", temp->data, temp->next );
...and is immediately followed by...
test(count); <-- Here you try to access the value of a local variable temp
which does
struct node * temp;
printf( "\n%d %u 2count\n", temp->data, temp->next );
To continue with the hotel-keys analogy here,
You go to a hotel and book a room.
your first time in a room you go ahead and set the air-conditioner to pleasant 20 degrees Celsius.
Later you check-out of the hotel. But immediately go back and book a room in the hotel again. The hotel gives you the same room (the key you had just returned was on the top of the stack of the keys at the concierge).
Now when you go back to the room a second time,
Would you be surprised if you checked the temperature of the air-conditioner (without setting it first) and found it to be 20 degrees Celsius?
The fact is the hotel, concierge (operating-system, C-library) made no guarantees that the air-conditioner settings (values on the stack) will be reset to some default value before giving you a room.
1. Of-course there will be hotels that provide such a service of reset-ing everything in the room to a default state before letting it out, and so will exist OSes/frameworks that scrub the stack between re-use.
2. A security-conscious person before checking out of his hotel room checks for any personal notes of his and ensures that a piece of paper with his bank-account number and mother's maiden name written on it isn't accidentally left back. Similarly if you are concerned about accidentally leaking any secret information by leaving it in a local variable on the stack then better set all local variables to 0
(or random values) before returning from the function.
3. The fact that the variable temp
seems to retain its value between 2 invocations of a function test()
is simply because the memory location used to hold temp
was the same during the 2 time test()
was invoked. Between the 2 invocations of test()
, the memory location was marked as free memory, but nobody else wrote to the memory location in between the 2 invocations of test()
.
4. In your example code, note that the actual data
and next
may be store on the heap (or wherever malloc()
allocates from). But the local pointer temp
continues to be on the stack.