Why do we need to delete allocated memory in C++ assignment operator?
<p>Why do we need the delete statement?</p>

<pre><code>const MyString&amp; operator=(const MyString&amp; rhs)
{ 
    if (this != &amp;rhs) {
        delete[] this-&gt;str; // Why is this required?
        this-&gt;str = new char[strlen(rhs.str) + 1]; // allocate new memory
        strcpy(this-&gt;str, rhs.str); // copy characters
        this-&gt;length = rhs.length; // copy length
    }
    return *this; // return self-reference so cascaded assignment works
}
</code></pre>
