-
new and delete keywords
I have a small question. If you use the new keyword but forget to delete, I know that there exists unfreed occupied space in the heap. Does that unfreed space remain that way forever, or is there a way to refree that area of the heap?
-
Tropical Coder
Once your program ends the OS should reclaim it.
-
Automatically? If that's the case, why is it so important to use delete?
-
Tropical Coder
Because if that new is in a function that gets called often, then you may run out of memory before your application ends. Imagine in a game that is redrawing the screen at say 70 fps. If every frame it new'd some memory but didn't delete it when it was done, your game would come to a halt pretty quickly ( well depending on amount of memory allocated). So basically if you are done with a chunk of memory, delete it.
-
Ok I get it. So you're saying that you need to delete your memory to keep your program running smoothly? And say that, just as in your example, you forget to delete and the program becomes very slow and the heap memory is overloaded, when you quit the program all the memory is refreed by the OS?
-
-
Popular pages
Recent additions