I have a question regarding memory allocation and deallocation in C++ when reassigning a variable. I've come across a situation where I'm using a stack within a loop and instead of creating a new empty stack for other purpose what will happen if i reassign it to a new empty stack without using the "new" keyword. I'm wondering about the memory management implications of this operation.
stack<int> st;
for (int i = 0; i < n; i++) {
while (!st.empty() && heights[st.top()] > heights[i]) {
// Some operations...
st.pop();
}
st.push(i);
}
st = stack<int>(); // Reassigning a new empty stack
Does reassigning a new value (in this case, an empty stack) to the variable st properly release the memory used by the previous stack? Or do I need to explicitly perform memory deallocation to ensure there are no memory leaks?
I'm using GCC version 10.2.1 with the standard C++ library. Any insights and best practices related to memory management in this scenario would be greatly appreciated. Thank you!