简体   繁体   中英

When creating a loop of new pointers, outputting the dereferenced pointer doesn't increase memory usage

I made a loop to try and run out of memory. Here's the weird part. When I run it like this, it works as expected and eats up the RAM, and goes over 1gb in seconds.:

while(true){
   int *pointer66 = new int;
}

However, when I add a cout like this, RAM usage stays consistent (it increases slowly dude to the console recording what the output is, but that's it, staying in the few megabytes):

while(true){
   int *pointer66 = new int;
   cout << *pointer66;
}

What is going on? It seems like the cout is preventing the memory leak? I'm using Visual Studio 2005.

Your assumption is wrong - std::cout cannot prevent memory leak. The reason might be that with console output your cycle runs orders of magnitude slower than without it. You may try to measure console output time and try to disassemble the resulting executables and find out that in both cases the memory is actually allocated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM