I have a question regarding when to use delete to free memory used by a pointer. I have a class function that uses dynamically allocated memory and I want to properly use delete to free up the space when I am done with it.

// Overloaded addition operator.
	CSimpleString operator+(const CSimpleString& aSimpleString) const
	{
		// Unsinged int variable that represents the length of both strings added together.
		size_t len = m_Length + aSimpleString.m_Length + 1;
		// Pointer to char.
		char* cstr = 0;
		// Allocate space for both strings + \0.
		cstr = new char [len+1];
		// String to hold left operand.
		string s1 = (pmessage);
		// String to hold right operand.
		string s2 = (aSimpleString.pmessage);
		// String to add both of them together.
		string s3;
		// Append both strings to s3.
		s3.append(s1);
		s3.append(s2);
		// Safe copy std::string to const char* 
		strcpy_s (cstr, len +1, s3.c_str());
		return CSimpleString(cstr);
		
	}

I just need to know where to place the delete statement and why I need to place it there.

Should it be in the class function before the return statement? In the class function after the return statement? Or in the main function? And where-ever I place it, why is that place the correct place?

The book I"m reading from doesn't really explain the correct placement of the delete statement. It would seem that I would need to use the delete statement in the class function because the dynamically allocated variable is local to the function but if I place it after the return statement will it still be executed?

Thank You.

You should delete only when you are done with this memory area.That means you can not delete in overloaded operator function.
In main you will recieve this memory address in another pointer.There if you are not passing it further to any other function and you are sure you won't use this memory area any more you can delete that pointer which will free up this memory allocated to variable cstr.

commented: good comment pointed me in the right direction +1

If you are afraid of delete, using auto_ptr can be a good option.

Why wouldn't I want to delete it in the function? At the end of the function I am using the newly created string as a parameter for the class constructor.

class CSimpleString
{
public:
	// Constructor definition.
	CSimpleString(const char* text = "Default Text")
	{
		pmessage = new char [strlen(text) + 1];
		strcpy_s(pmessage, strlen(text) + 1, text);
		m_Length = strlen(pmessage);
		
	}
	
	// Overloaded addition operator.
	CSimpleString operator+(const CSimpleString& aSimpleString) const
	{
		// Unsinged int variable that represents the length of both strings added together.
		size_t len = m_Length + aSimpleString.m_Length + 1;
		// Pointer to char.
		char* cstr = 0;
		// Allocate space for both strings + \0.
		cstr = new char [len+1];
		// String to hold left operand.
		string s1 = (pmessage);
		// String to hold right operand.
		string s2 = (aSimpleString.pmessage);
		// String to add both of them together.
		string s3;
		// Append both strings to s3.
		s3.append(s1);
		s3.append(s2);
		// Safe copy std::string to const char* 
		strcpy_s (cstr, len +1, s3.c_str());
		cout << &cstr << endl;
		return CSimpleString(cstr);
		// delete [] cstr;
	}

	// Destructor definition.
	~CSimpleString()
	{
		cout << "Destructor Called." << endl;
		delete [] pmessage;
	}
private:
	size_t m_Length;
	char* pmessage;

};
};

The class destructor frees up the memory allocated in the constructor and I can't place the delete statement in the class destructor because the compiler doesn't know about the variable, it's out of scope just like it would be if I tried to place the delete statement in the main function.

So this leads me back to my original thought that I need to delete it within the scope of where I declared and allocated it. However, my question still stands if I place the delete statement after the return statement is the delete statement actually getting executed?

The program compiles and executes and performs as expected I'm just wondering if the delete statement is actually being performed and what will happen if its not and is there anyway to test this?

Of course, if you put the delete statement after the return statement, it won't be executed at all. Instead you can do like

CSimpleString mySimpleString(cstr);
delete[] cstr;
return mySimpleString;

commented: Great comment was exactly what I was looking for. +1

I don't understand why your are interchanging null terminated c style strings with the String class. The string class alone should suffice here and it handles its own memory (and also contains an overloaded + operator). If you are trying to learn about memory management, write the whole code using only char arrays, pull your hair out a bit, then never use them again.

commented: Great comments, helped me with the thought process. +1

Thank you everyone. skatamatic I didn't write the problem it was just a simple exercise at the end of a chapter to design a class that took a const* char as a parameter and had to have certain requirements. I agree, I thought it was a pretty stupid and I did pull my hair out a bit.

I was really changing null terminated strings with std::strings because I read an article on codeproject.com that covered string types and conversions really well and I wanted to kind of throw that in here too. And kbshibukumar thank you, that should have been obvious to me, I feel rather dumb right now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.