'Why is there increase in heap size when using string stream despite full deallocation?
Using the VS (Community 2022 & Windows 10) diagnostic tool->Memory Usage (Heap), we see an increase in heap size of 0.46kb when reading input at: "stream >> temp;". This increase persists to the end of the program as per +0.46kb reported at "return 0;". No memory leaks are reported by either of the _CrtDumpMemoryLeaks() calls.
What is the reason for this increase in heap usage? (The heap size has been a great way to verify memory management up-to now, usage of std::cout has a similar effect as an increase in allocation that lasts until program end)
(The allocations and deallocations are included to avoid compiler optimizations)
The following code compiles on Visual studio community edition 2022 in both C++14 and C++20.
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#define _CRTDBG_MAP_ALLOC_NEW
#include <crtdbg.h>
#include <assert.h>
#endif
#include <sstream>
void debug() {
std::stringstream stream;
stream << "5.2 22.1 4.2 5.5\n";
double* ptr{ nullptr };
while (stream) {
double temp{ 0 };
stream >> temp;
if (stream) {
ptr = new double;
delete ptr;
}
}
return;
}
int main() {
{
debug();
_CrtDumpMemoryLeaks();
}
_CrtDumpMemoryLeaks();
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
