My question is about the absolute scope of memory allocated on the heap. Say you have a simple program like
class Simple
{
private:
int *nums;
public:
Simple()
{
nums = new int[100];
}
~Simple()
{
delete [] nums;
}
};
int main()
{
Simple foo;
Simple *bar = new Simple;
}
Obviously foo
falls out of scope at the end of main
and its destructor is called, whereas bar
will not call its destructor unless delete
is called on it. So the Simple
object that bar
points to, as well as the nums
array, will be lost in the heap. While this is obviously bad practice, does it actually matter since the program ends immediately after? Am I correct in my understanding that the OS will free all heap memory that it allocated to this program once it ends? Are the effects of my bad decisions limited to the time it runs?