Function pop out of stack

when does a function gets popped out from stack…a) when it hits return or b) when the final curly backets are closed…?

hey @ynikhil1999, in Both cases it popped out,whichever occurs first.

if this is true…

then why we are not able to return the address of a static variable in a function call?(why we have to do dynamic memory allocation to do so?)

hey @ynikhil1999, we can return the address of static variable. Check this https://ide.codingblocks.com/s/96923

check out this code sir…

the output is zero because the compiler doesn’t allow to return the address…

hey @ynikhil1999, you ask me for static variables.

that array is not dynamic…that’s static right.?

hey @ynikhil1999, array can be dyanamic also.
Coming to your doubt, you are getting 0 because that array is local to demo function and when function gets over this array got poped out of call stack and when you tried to print its address in main function, you got 0 as address.

Local variables are placed in the stack. Once the closing braces of that function is executed, the stack is deallocated.

Returning address of local variable is never be a good idea at all

1 Like

hey @ynikhil1999 , if your query is resolved. Please mark this doubt as resolved and rate me on the basis of your experience.
rating option will appear when to mark this doubt as resolved

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.