Does the memory allocated to a local variable get deallocated automatically after its scope ends? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Does the memory allocated to a local variable get deallocated automatically after its scope ends?

10th Sep 2016, 3:03 PM
Swagata
Swagata - avatar
4 Answers
+ 2
Unless the variable is static, memory will be deallocated, thus losing its value at the end of its scope. Of course, when calling again, that memory is reallocated.
10th Sep 2016, 4:58 PM
Cohen Creber
Cohen Creber - avatar
+ 1
Or is it reallocated at each call and deallocated after the scope ends ?
10th Sep 2016, 3:37 PM
Swagata
Swagata - avatar
+ 1
Actually it depends. If you just declare a variable inside a function (int b = 3;), the memory assigned to that variable will be freed when it goes out of scope. If you use new (int *b = new int[3];), the memory has to be freed explicitly by using delete (delete [] b;).
11th Sep 2016, 12:56 PM
Andrei Timofte
Andrei Timofte - avatar
0
no... basically it remains to that memory so that you can recall it at any time during program
10th Sep 2016, 3:28 PM
Shrinivas Deshpande
Shrinivas Deshpande - avatar