0
Can someone explain this code in terms of memory allocation?
my doubt is since a is local variable ,&the function is returning the pointer which is pointing to local variable which will get deleted once when exit from function ,so why it is returning the value of a,or it can return any value? https://code.sololearn.com/cd8qCiw8LR4N/?ref=app please explain in term of memory allocation eg:stack
5 Answers
+ 2
Its possible and depends on OS, compiler (architecture and optimization)... This happen basically because stackframe is not "cleared" for optimization when you return an adress where value its same but can be overwrited soon... C/C++ allow you much but this dont mean that its right because i suggest you to considerate this like undefined behaviour and not do this in real code
+ 2
Ok... This is a more simple resource http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/
+ 1
thank u KrOW BUT What is the meaning of "stack frame is not cleared for optimisation" please explain in simple language
+ 1
thank u I will try to understand