Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if I didn't call delete operator after allocating data using new and ending program?

What happens if I didn't call delete operator after allocating data using new. I know that the data that has been allocatted, won't be available until releasing it, but after ending the program ?

Why the PC seems to have nausea xD, i mean that it is very slow but after a while its performance become better but not like before the program execution ?

Note: I'm running windows XP.

like image 237
Sherif Avatar asked Apr 22 '11 12:04

Sherif


People also ask

What happens if you dont call delete C++?

In C++, delete will result in the destructor of the object being called. This destructor may take care of things other than releasing memory. It may close files, reduce reference counts, etc. So there is no telling what may go wrong if you neglect to delete a dynamically allocated object.

What actually happens when you use Delete on something allocated with new []?

The code calls operator new[] to allocate memory for 10 string object, then call the default string constructor for each array element. In the way, when the delete operator is used on an array, it calls a destructor for each array element and then calls operator delete[] to deallocate the memory.


5 Answers

When the program ends all the memory it requested (stack, heap whatever) is claimed by the operating system.

like image 59
cnicutar Avatar answered Oct 02 '22 05:10

cnicutar


I think you're seeing the effects of evicting useful programs from main memory to the disk.

Your intentionally-leaky program is trying to allocate all memory on the system. To satisfy your program's demand, Windows is finding other programs on the system, writing their memory to the page file, and reallocating their memory to your program. When you see the page file usage jump to maximum, it's because most other programs have been shoved there instead of main memory.

When your program exits, Windows reclaims all the program's memory (as noted by others). But all the other programs on your computer still have their memory saved in the page file on disk, not in main memory. So when they run, Windows has to go load their memory pages from disk, making the program appear slow. After a while, the programs will move back to main memory, and performance will look like normal.

like image 22
Karmastan Avatar answered Sep 30 '22 05:09

Karmastan


Suppose you wrote your own version of the 'ls' command, and the memory management was so poor that it leaked 10 MB every time it ran, which is a large leak. Does this matter? Not really. The system will reclaim all memory when the program exits, which is most likely a fraction of a second after it starts. Sure, your pride is affected, and the craftsmanship would be low, but the system doesn't suffer. It is likely the user would never know how poorly the memory is managed.

Now suppose you wrote your own version of Apache. It is expected to run for months at a time without restart, so even if it leaked a small amount of memory, that would accumulate over time and cause a problem. The user would likely know about this one. The sysadmins certainly would.

So to summarize, the OS does the right thing and reclaims memory. Leaky software is bad. But there are cases where it doesn't matter much.

like image 26
Paul Beckingham Avatar answered Oct 01 '22 05:10

Paul Beckingham


All the memory your program allocated, but failed to release, is automatically released when your program ends. That is, after your program finishes it no longer has any effect or influence on the OS.

like image 35
Pete Wilson Avatar answered Sep 29 '22 05:09

Pete Wilson


The heap and the stack memory are allocated to each process. Dynamic allocations happen on the heap. Each executable or program runs in a process. So once the program ends the heap and the stack allocated for that process are returned back to the OS. This includes the leaking dynamic memory.
Once the program has ended, It should have no bearing on performance of the OS.

like image 26
Alok Save Avatar answered Sep 30 '22 05:09

Alok Save