Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question About Delete Operator 1

Status
Not open for further replies.

Brianw79

Programmer
Dec 6, 2002
3
US
My question is about a block of code I saw in particular C++ book. The following is the code:

string& string::eek:perator = (const string& Str)
{
char* temp = new char[Str.m_Length];

m_Length = Str.m_Length;
memcpy(temp,Str.m_Data,m_Length);
delete [ ] m_Data;

m_Data = temp;

return *this;
}

This is a constructor for a home made string class. I understand most of the code in this block but I had a question as to why the author would use the delete operator on m_data and then immediatly after deleting it use it to hold something else. Wouldn't you have to issue another New statement in order to use m_data again? I would think if you didn't use New again that you couldn't be sure m_data would always but to a valid address.

As you can probably tell I am fairly new to C++, although I have been programming for a long while in other languages. I know this isn't really a "problem" so much as it is just a question I had to help my understanding of C++. Any help would be greatly appreciated.
 
Attribute m_Data of class string is a pointer on characters, or can be considered as an dynamic array of characters.
Code:
delete[] m_Data;
What is pointed by m_Data is deleted and corresponding space memory is made available for new memory allocations.
Code:
char* temp = new char[Str.m_Length];
//...
m_Data = temp;
//...
After that the space memory pointed by m_Data was freed, the m_Data pointer points to the new allocated character array, denoted by temp. Since it is a valid space memory, m_Data doesn't need to be reallocated.

--
Globos
 
You don't have to allocate new memory directly between freeing the old memory and assigning the pointer the new memory.

It's just like the following: you don't need to take off your old shirt, go buy a new one, then put it on in that order. You can buy the new shirt ahead of time.


That's what happens in your example:

You allocate the new memory and keep track of it with a temporary pointer. (You buy a new shirt).

Then you delete the old memory from the non-temporary pointer. (Take off your old shirt... and delete it).

Then you point the non-temporary pointer at the location of the newly allocated memory you've been using the temporary to keep track of. (Put on the new shirt).


Think about the following: what happens if you assign one of these strings to itself?

As written, it works. It's not very efficient, but it works.

But if you deleted the memory before allocating new memory and performing the copy, you'd just end up wiping out the string because you didn't save the copy of what it looked like.
 
Wow, you guys work quick. Thank you for the explanations, they really helped!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top