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.
// 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 = newchar[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?
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.
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.
// 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 = newchar[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?
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.
Last edited by skatamatic; Mar 5th, 2009 at 1:32 pm.
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.
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.