Can someone explain this code in terms of memory allocation? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

15th Jun 2018, 12:45 PM
Bahubali
Bahubali - avatar
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
15th Jun 2018, 9:54 PM
KrOW
KrOW - avatar
16th Jun 2018, 1:23 PM
KrOW
KrOW - avatar
+ 1
thank u KrOW BUT What is the meaning of "stack frame is not cleared for optimisation" please explain in simple language
16th Jun 2018, 12:09 AM
Bahubali
Bahubali - avatar
+ 1
thank u I will try to understand
16th Jun 2018, 12:40 PM
Bahubali
Bahubali - avatar