Erik Engheim
Nov 28, 2021

--

The value will stay in the same spot in the stack the whole time. This allows you do pass pointers to this value to other functions you call. These pointers will remain valid through the whole execution time of these nested function calls, because these calls have a shorter lifetime than the value on the stack.

The problem is if you returned a pointer from a value on a stack, because when you return from a function you pop the stack. Thus any values created within a function call, will get released/removed if they got stack allocated.

I am sorry this isn't a very clear explanation. I think I need an illustration to describe it better. You could read up on activation frames or stack frames to get a better idea, unless you already know this.

--

--

Erik Engheim
Erik Engheim

Written by Erik Engheim

Geek dad, living in Oslo, Norway with passion for UX, Julia programming, science, teaching, reading and writing.

Responses (1)