From: "Abhijit Menon-Sen" <[EMAIL PROTECTED]>

> At 2004-03-04 12:24:25 +0530, [EMAIL PROTECTED] wrote:
> >
> > According to me, as we have "string" as local variable, it should
vanish.
> > But, i am still able to return and print the string "hello world".
>
> Returning a pointer to local variables is undefined; that is, there is
> no guarantee whatsoever about its behaviour. In particular, it doesn't
> need to "vanish" (but it may).
    By vanish I mean, function gone it's data gone. Each function data
(unless got through malloc or defined static) is put on it's     stack
frame, so when function returns the stack frame is popped out. So local
variable no more exists !!!

> So your code works only by accident. I'm surprised that gcc's warnings
> are defeated by your intermediate assignment (ptr = string), but only
> a little.
>
> > When I change to :
> >     char string[200]="hello world";
> >    and do not use strcpy(),
> > It shows junk, the behaviour is as per my expectations.
>
> (Coincidence. As it happens, it works for me even without the strcpy.)
    This has been my understading, but now the problem is - "if all what i
know is correct, yaar yeh kaam kyon kar raha hae".
    I have tried it so many times ... but if it's just coincidence, what a
beautiful coincidence    :-)
    I am also amused, that's why I thought of really knowing WHY !!!! and
posted it.
    I have compiled using -Wall option of gcc, but in vain, gcc doesn't
complain. Is this a weired behaviour by gcc or it's what     is expected ???
    thanx ams. i m waiting for more info ...

    Vikas



_______________________________________________
ilugd mailinglist -- [EMAIL PROTECTED]
http://frodo.hserus.net/mailman/listinfo/ilugd
Archives at: http://news.gmane.org/gmane.user-groups.linux.delhi 
http://www.mail-archive.com/[EMAIL PROTECTED]/

Reply via email to