memory allocation

If I have the follow class definition:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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(const char* text = "Default message")
	{
		pmessage = new char[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?
Last edited on
No once a program is done executing, any memory it had allocated is freed up. But this isn't really an excuse to not manually free memory.
closed account (zb0S216C)
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."

Wazzak
Last edited on
Thanks for the info. I wasn't looking for an excuse to not write a destructor, I was just curious.
Topic archived. No new replies allowed.