In C++, when you make a new variable on the heap like this:
int* a = new int;
you can tell C++ to reclaim the memory by using delete like this:
delete a;
However, when your program closes, does it automatically free the memory that was allocated with new?
Yes, it is automatically reclaimed, but if you intend to write a huge program that makes use of the heap extensively and not call
deleteanywhere, you are bound to run out of heap memory quickly, which will crash your program.Therefore, it is a must to carefully manage your memory and free dynamically allocated data with a matching
deletefor everynew(ordelete []if usingnew []), as soon as you no longer require the said variable.