Resizing a List, I get segmentation fault
<p>I have implemented a List (different from standard c++ list), this is for exercise.
The problema is that after resizing the list, I get segmentation fault.
I have marked the linke when I get seg. fault, I don't know the reason.</p>

<p>The purpose of resize method is to resize the list: let's say that I have a list of 2 integers, if I resize it to 5 I'll get other 3 elements, all initialized to zero.
If instead I have a list of 4 elements, and I resize it to 2, 2 elelements will be cut.
If I have a int list l, l[0] is the first object of the list, l[1] the second...
So I crash when i attempt to resize the list, exactly in the line that I have marked.</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;cassert&gt;

using namespace std;

template&lt;class T&gt;
class List
{
public:
    List&lt;T&gt;();
    ~List&lt;T&gt;();
protected:
    List&lt;T&gt; (T info);
public:
    int size();
    void resize(int length);
    void add(T info,int i);
    void push(T info);
    T pop();
    void remove(int i);
    T extract(int i);
    T get(int i);
    friend ostream&amp; operator&lt;&lt; (ostream&amp; out, List&lt;T&gt;&amp; l)
    {
        List&lt;T&gt;* ptr=l.next;
        if(l.info==NULL)
        {
            while(ptr!=&amp;l)
            {
                out &lt;&lt; *(ptr-&gt;info) &lt;&lt; "\t";
                ptr=ptr-&gt;next;
            }
        }
        else
        {
            out&lt;&lt; *(l.info) &lt;&lt;"\t";
        }
        return out;
    }
    friend istream&amp; operator&gt;&gt; (istream&amp; in,List&lt;T&gt;&amp; l)
    {
        in &gt;&gt; (*l.info);
        return in;
    }
    bool operator== (List&lt;T&gt;&amp; r)
    {
        bool result=true;
        List&lt;T&gt;* p1,*p2;
        p1=this;
        p2=&amp;r;
        assert((p1-&gt;info==NULL &amp;&amp; p2-&gt;info==NULL)||(p1-&gt;info!=NULL &amp;&amp; p2-&gt;info!=NULL));
        if(p1-&gt;info==NULL)
        {
            p1=p1-&gt;next;
            p2=p2-&gt;next;
            result=(p1-&gt;length == p2-&gt;length);
            while(result &amp;&amp; p1!=this)
            {
                result=(*(p1-&gt;info)==*(p2-&gt;info));
                p1=p1-&gt;next;
                p2=p2-&gt;next;
            }
        }
        else
        {
            result=(*(p1-&gt;info)==*(p2-&gt;info));
        }
        return result;
    }
    List&lt;T&gt;&amp; operator+= (List&lt;T&gt;&amp; l)
    {
        List&lt;T&gt;* ptr;
        ptr=l.next;
        while(ptr!=&amp;l)
        {
            this-&gt;push(*(ptr-&gt;info));
            ptr=ptr-&gt;next;
        }
        return *this;
    }
    List&lt;T&gt;&amp; operator[] (int i)
    {
        List&lt;T&gt;* ptr;
        ptr=this;
        assert(i&lt;length);
        for(int j=0;j&lt;=i;j++)
            ptr=ptr-&gt;next;
        return *ptr;
    }
private:
    T *info;
    List &lt;T&gt;* next;
    List &lt;T&gt;* prev;
    int length;
};

template &lt;class T&gt;
List&lt;T&gt;::List ()
{
    info=NULL;
    next=this;
    prev=this;
    length=0;
}

template&lt;class T&gt;
List&lt;T&gt;::List (T info)
{
    next=this;
    prev=this;
    this-&gt;info=new T();
    *(this-&gt;info)=info;
}

template&lt;class T&gt;
List&lt;T&gt;::~List()
{
    List&lt;T&gt;* ptr, * temp;
    ptr=this-&gt;next;
    if(this-&gt;info==NULL)
    {
        while(ptr!=this)
        {
            delete ptr-&gt;info;
            temp=ptr;
            ptr=ptr-&gt;next;
            delete temp;
        }
    }
}

template&lt;class T&gt;
int List&lt;T&gt;::size()
{
    return length;
}

template&lt;class T&gt;
void List&lt;T&gt;::resize(int length)
{
    assert(length&gt;=0);
    if(this-&gt;length&lt;length)
    {
        for(int i=this-&gt;length;i&lt;length;i++)
            push(0); 
    }
    else
    {
        for(int i=length;i&lt;this-&gt;length;i++)
            extract(this-&gt;length-1); // here I call exctract, extract calls
    }                                // remove, remove calls get
}

template&lt;class T&gt;
void List&lt;T&gt;::add(T info,int i)
{
    List&lt;T&gt;* ptr,*t1,*t2;
    length++;
    ptr=&amp;((*this)[i]);
    t1=ptr-&gt;prev;
    t2=ptr;
    ptr=new List&lt;T&gt;(info);
    t1-&gt;next=ptr;
    ptr-&gt;prev=t1;
    t2-&gt;prev=ptr;
    ptr-&gt;next=t2;
}

template&lt;class T&gt;
void List&lt;T&gt;::push(T info)
{
    add(info,length-1);
}

template&lt;class T&gt;
void List&lt;T&gt;::remove(int i)
{
    List&lt;T&gt;* ptr,*t1,*t2;
    ptr=&amp;((*this)[i]);
    t1=ptr-&gt;prev;
    t2=ptr-&gt;next;
    delete ptr;
    t1-&gt;next=t2;
    t2-&gt;prev=t1;
    length--;
}

template&lt;class T&gt;
T List&lt;T&gt;::extract(int i)
{
    T temp=this-&gt;get(i);
    remove(i);
    return temp;
}

template&lt;class T&gt;
T List&lt;T&gt;::get(int i)
{
    assert(i&lt;length);
    return *(this[i].info);         // here i have seg. fault !!!!!!!!!!
}                                   // I cannot figure why ...

template&lt;class T&gt;
T List&lt;T&gt;::pop()
{
    return extract(length-1);
}

int main(int argc, char **argv) 
{
    List&lt;int&gt; l;
    l.resize(3);
    for(int i=0;i&lt;3;i++)
        cin&gt;&gt;l[i];
    l.resize(2);
    cout&lt;&lt;l&lt;&lt;endl;
    return 0;
}
</code></pre>

<p>I hope it's not a too kilometric doce, thanks in advance for who will read and try to help me.</p>
