Are local variables in function automatically deleted when the function terminates? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Are local variables in function automatically deleted when the function terminates?

Are local non-pointer variables in functions automatically deleted when the function stops or they continue to occupy memory in the stack? If I use the same function many times does the occupied space increase?

21st Feb 2019, 10:45 PM
Enrico Pedretti
Enrico Pedretti - avatar
1 Answer
+ 3
Yes they will be deleted. Every time you call a function you put a "stack frame" on top of the stack where all the local variables live in, and when the function is done, the stack frame will be "teared down". The stack is also usually limited in size. If you keep tacking too many frames onto the stack - for example when a recursive function goes rogue and keeps calling itself - then a "stack overflow" occurs and your program blows up.
21st Feb 2019, 11:46 PM
Schindlabua
Schindlabua - avatar