class CMessage
{
private:
char* pmessage; //pointer to object text string
public:
//function to display a message
void ShowIt() const
{
cout << endl << pmessage;
}
//Constructor definition
CMessage(constchar* text = "Default message")
{
pmessage = newchar[strlen(text) + 1]; //Allocate space for text
strcpy_s(pmessage, strlen(text) + 1, text); //copy text to new memory
}
};
I understand that with no deconstructor for the class, when objects are deleted the memory isn't freed up, so that as more and more objects are created and deleted, eventually there will be no more memory available. What I'm wondering is: when the program finishes execution, does the memory get freed up? If I am able to run the entire program successfully, do I risk running out of memory on subsequent executions until I eventually have to reboot windows to get that memory back?
This isn't C#, Java, nor is it CLR. There's no garbage collection system in standard C++. new/new[] has an corresponding delete/delete[] for a reason, so use it. Also, a class's trivial destructor is over-writeable for a reason; exploit that feature. If you can't be bothered to free up the memory, use auto pointers. An auto-pointer is a class that encapsulates a pointer which stores allocated memory. The trivial destructor is overridden to free the memory it encapsulates. I guess you can think of auto-pointers as: "If you don't do it, someone has to."