i'm trying to understand how C++ works. When u declare a new variable (int x) within a loop, for example within a for-loop. Memory is allocated to the variable x within the loop, but what happens to that memory after exiting the for-loop? My understanding from a friend is that Java will automatically de-allocate the memory, but what about C++?
Thanks.
It will be deallocated if declared on stack (i.e. via int x = ...
) and when the variable leaves its scope. It won't be deallocated if declared on heap (i.e. via int *x = new int(...)
). In that case you have to explicitely use delete
operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With