Destructor causing segmentation fault
<p>I have implemented a class string, similar to std::string one.
I have a problem when the destructor is called: the field length has the length of the characters allocated in field.
This is the class:</p>

<pre><code>class indexException:public std::exception
{
    public:
    virtual const char* what()
    {
        return "Index is either too long, or negative";
    }
};

class string
{
    public:
    static const unsigned int length_max=100;
    string(const char* field=NULL)
    {
        if(field!=NULL)
        {
            const unsigned int length=strlen(field);
            this-&gt;field=new char[length+1];
            this-&gt;length=length;
            for(unsigned int i=0;i&lt;=length;i++)
                this-&gt;field[i]=field[i];
        }
        else
        {
            this-&gt;field=NULL;
            length=0;
        }
    }
    string(string&amp; str)
    {
        string(str.field);
    }
    ~string()
    {
        if(length&gt;0)
            delete field;
    }
    char&amp; operator[] (int i) const throw()
    {
        try
        {
            if(i&lt;0 || i&gt;=(int)length)
                throw indexException();
        }
        catch(indexException&amp; e)
        {
            std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
        }
        return field[i];
    }
    string&amp; operator=(const char* field)
    {
        const unsigned int length=strlen(field);
        if(this-&gt;length&gt;0)
            delete this-&gt;field;
        this-&gt;field=new char[length];
        this-&gt;length=length;
        for(unsigned int i=0;i&lt;length;i++)
            this-&gt;field[i]=field[i];
        return *this;
    }
    string&amp; operator= (const string&amp; str)
    {
        if(this!=&amp;str)
            *this=str.field;
        return *this;
    }
    operator char* ()
    {
        return field;
    }
    friend std::ostream&amp; operator&lt;&lt; (std::ostream&amp; out, string&amp; str);
    friend std::istream&amp; operator&gt;&gt; (std::istream&amp; in, string&amp; str);
    public:
    unsigned int length;
    char* field;
};

std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, string&amp; str)
{
    out &lt;&lt; str.field;
    return out;
}

std::istream&amp; operator&gt;&gt; (std::istream&amp; in, string&amp; str)
{
    char temp[string::length_max];
    in &gt;&gt; temp;
    str=temp;
    return in;
}
</code></pre>

<p>If I use the assignment operator, this doesn't cause a segmentation fault.
But it undirectly cause it.
I explain how:</p>

<pre><code>int main(int argc,char** argv)
{
    string str="hi";
    string str2=str;
    return 0;
}
</code></pre>

<p>Putting a breakpoint into the assignment operator overloading, I realized that the assigment operator doesn't cause segmentation fault.
The problem is after, when exiting from main.
If I remove the destructor I don't get this segmentation fault, but I would know why I get this problem.</p>

<p>Edit: I have understood where's the problem.
I followed your suggestions but it still goes to segmentation fault.
But now it doesn't crash anymore on the destructor method, but on the assignment operator overloading:</p>

<pre><code>    string&amp; operator=(const char* field)
    {
        unsigned int length=0;
        if(field!=NULL)
            length=strlen(field);
        else
            field="";
        if(this-&gt;length&gt;0)
            delete[] this-&gt;field;
        this-&gt;field=new char[length+1];
        this-&gt;length=length;
        strcpy(this-&gt;field,field);
        return *this;
    }
</code></pre>

<p>The problem is when I delete this->field, the debugger stops there.
An example of segmentation fault:</p>

<pre><code>string str("hi");
string str2=str;
</code></pre>

<p>This causes segmentation fault.I suppone it's because str2 is not initialized, and length has an undefined value.
If I instead do this:</p>

<pre><code>string str("hi");
string str2;
str2=str;
</code></pre>

<p>There isn't any segmentation fault.Why?
I thought that calling :</p>

<pre><code>string str2;
</code></pre>

<p>Was also calling the constructor, or is that the "=" operator has the precedence?
How to solve this?</p>

<p>PS: I also changed other things,like the copy constructor.
Full code is here:
<a href="http://pastebin.com/ubRgaVr8" rel="nofollow">http://pastebin.com/ubRgaVr8</a></p>

<p>Solved: I changed the copy constructor as suggested in the accepted reply:</p>

<pre><code>    string(const string&amp; str)
    {
        length=str.length;
        field=new char[str.length+1];
        memcpy(field,str.field,length+1);
    }
</code></pre>
