Question

#include<stdio.h>

void bar()
{
 int a=4;
}
void foo()
{
 int a;
 printf("%d",a);
}
int main()
{
 bar();
 foo(); 
}

I'm sure that the above program gives the output as some junk value(Thats what happenes when i compile and run). But I read in an article that it is possible for some compilers to give the output as 4 itself. The explanation given was that it is related to activation stack and activation frames. I dont understand. Is it something like the value of int a=4 from bar() function is stored somewhere in the activation stack for future use??I'm confused!!

Was it helpful?

Solution 2

To be clear it is possible that foo's a will end up with the 4 in it that bar set there (nothing to do with them having the same name BTW). But this is highly dependent on the compiler machines etc.

What you are actually doing (in foo) is 'undefined behavior', you use a without giving it a value. Undefined means the compiler may do anything, format your harddrive, print any random value including you best friends birthdate, or it could print 4.

Why might it be 4, because the way stack frames are built on many systems will results in bar writing 4 to an address and foo reading it back. The 2 functions have the same paramters, return type and number of locals so its possible that the first local will be allocated in the same memory location. THis is why you read that they could have the same value, but this is by accident not by design

BTW did you get 4 or not, probaly not

OTHER TIPS

Don't worry about what some compilers might do. It's called "undefined behavior". Always make sure you initialize your variables to whatever you want them to be.

So again, it's pointless to try to "understand" because there's no logic, and it's certainly not "expected behavior". But, it's conceivable that some compilers would reuse a register on the cpu if the functions are inlined, so yes it could happen. If you're concerned somehow about someone running a function after yours and "stealing your value", then just set it to zero before exiting your function. But that is not "Best Practices".

In other words, even if the second function used 'b' instead of a, it could have 4 in it if they are reusing the memory space. Or some other compiler might actually preallocate in alphabetical order, so only 'a' would work. Again, it's undefined behavior.

Function foo uses an uninitialised variable. The compiler is free to do what it likes in this situation. In practise, you might get a variable that was previously placed on the stack in a now defunct frame. It could be 4, as you say, but it could be anything else instead. Never rely on this sort of behaviour...it could be 99% reliable but go wrong on the 100th try.

Although the two variables have the same name, they are stored in different addresses and the second a that was initialized by compiler has possibly another value than 4.

Here it looks like a, but once compiled, those two local variables are different and can't be the same.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top