I thought that once a function returns, all the local variables declared within (barring those with static keyword) are garbage collected. But when I am trying
It's undefined behavior, anything can happen, including appearing to work. The memory probably wasn't overwritten yet, but that doesn't mean you have the right to access it. Yet you did! I hope you're happy! :)