0

we know that when the ownership of a variable moved, the variable is not valid anymore until the end of the scope when its' space become free in stack. but what happened to the memory space that becomes invalid? does it waste until the end of scope?

I don't have any idea of how OS managing the stack

1 Answers1

0

The OS does not manage the stack (besides allocating and possibly growing it), the program does.

Putting that aside, yes, the space is wasted, but usually this is not a problem. Also, the compiler can sometimes optimize that and re-use the same space for other variables, if their lifetime do not overlap.

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77