Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   a urget help from someone!!!!!!! (http://www.go4expert.com/forums/urget-help-t19732/)

vignesh1988i 10Oct2009 15:29

a urget help from someone!!!!!!!
 
int * fun();
void main()
{
int *l;
clrscr();
l=fun();
printf("hi : ");
printf("%d",*l);
getch();
}
int *fun()
{
int j=90;
return &j;
}

pll.... say the output for this with a proper justifications !!!!!!!!!:smug::snore:

shabbir 10Oct2009 17:59

Re: a urget help from someone!!!!!!!
 
What is your expectation of the output and did you try running it ?

vignesh1988i 11Oct2009 08:53

Re: a urget help from someone!!!!!!!
 
YA of course.... sir :) it's printing some garbage value... i know it will print garbage value... but I need to know the reason why it's printing so....

And if i don't place that printf() statement , the value is correctly printing as 90...... so.. plz.. state me the reason .... sir... thank u:nice:

shabbir 11Oct2009 09:12

Re: a urget help from someone!!!!!!!
 
You have to understand the memory mapping for that. I would try to explain. You are returning address of a variable which is out of scope which means you are returning memory address which can be allocated later and now when you call the printf with hi as parameter the returned memory is being filled by some value

xpi0t0s 11Oct2009 15:10

Re: a urget help from someone!!!!!!!
 
Yep, the output is undefined because j is local to fun() and its address is no longer valid when the function returns.

So if the program needs to return the address of a new integer then you should create a new one on the heap with new or malloc, and remember somewhere in the program to delete/free it when you've finished with it, otherwise you get a memory leak.

vignesh1988i 12Oct2009 08:34

Re: a urget help from someone!!!!!!!
 
sir , how can it be invalid when the address returns to the main() function....... the main problem is the printf() statement , if i give that it's printing statement inbetween these it's printing some garbage value ,.... if i directly print as *j , the value is correcly printing....:disappoin:disappoin

for shabbir sir .. sir kindly explain me in detail ,how the memory mapping is done >... :)

xpi0t0s 12Oct2009 12:45

Re: a urget help from someone!!!!!!!
 
OK, the address itself isn't invalid, what is invalid is the assumption that j still exists. C and C++ pass arguments to functions by placing them on the stack, and local (automatic) variables are also created on the stack. So what your program does is to call fun() with no parameters; fun() creates j on the stack, then when it returns its stack frame and everything in it is discarded (although not necessarily changed). The main function then calls printf with a parameter "hi" which overwrites the memory that j previously occupied. Then you call printf a second time with two parameters which overwrite the memory that j previously occupied for the second time.

So that is why the program displays garbage for j. It's because you are assuming that j still exists, and that assumption is wrong. The problem is NOT the printf statement - both of those are perfectly fine.


All times are GMT +5.5. The time now is 05:46.