0

I have a struct A.and in this struct,there is a list <struct A> and some pointers.

 struct A{
    char *p;
    int a;
    list<A> list;
    A(char *s, int b)
    {
        cout << "constructor:" << b << endl;
        p = s;
        a = b;
    }
    A(const struct A &right)
    {
        cout << "copy constructor" << right.a << endl;
        int len = strlen(right.p)+1;
        p = new char[len];
        memset(p, 0, len);
        memcpy(p, right.p, len);
        a = right.a;
    }
    void insert(A &other)
    {
        list.push_back(other);
    }
    ~A()
    {
        cout << "~sss(): " << a << endl;
        delete[] p;
        p = NULL;
    }
};

when insert a instance to list in the function fun,there is a heapfree error happening after calling destructor,the function fun as follow:

void fun(A &other){
    A f("ssss", 1);
    other.insert(f);
}

i search this in google,and all say push_back() will insert a copy of the object,so need to call copy constructor.when there is no copy constructor, it will call default copy constructor to do shallow copy, but the struct has one.is there any other cause? thanks a lot!

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61

0 Answers0