简体   繁体   中英

segmentation fault at the end of the destructor on deleting integer pointer

I am trying to understand the below program . While executing am getting errors as shown below.

#include<iostream>
using namespace std;
class Base
{
public:
    int *a;
    int a1;
    int b;


    Base(){
        cout<<"Inside Constructor"<<endl;
        a1 = 10;
        a = &a1;
        cout<<"   "<<a<<endl;
        b = 20;
    }

    Base(const Base &rhs)
    {
        cout<<"Inside Copy Constructor"<<endl;
        a = new int;
        *a = *(rhs.a);
        b = rhs.b;
    }

    ~Base(void)
    {
    cout<<"Inside destructor"<<endl;
    delete a;
    }
};


int main()
{
Base obj;
Base obj2(obj);
cout<<"obj a "<<*(obj.a)<<" b "<<obj.b<<endl;
cout<<"obj2 a "<<*(obj2.a)<<" b "<<obj2.b<<endl;
obj.a1 = 30;
obj.b = 40;
cout<<"obj a "<<*(obj.a)<<" b "<<obj.b<<endl;
cout<<"obj2 a "<<*(obj2.a)<<" b "<<obj2.b<<endl;
return 0;
}

While executing this code i am getting the following output

Inside Constructor
Inside Copy Constructor
obj a 10 b 20
obj2 a 10 b 20
obj a 30 b 40
obj2 a 10 b 20
Inside destructor
Inside destructor
Segmentation fault

[EDIT] I was looking for a way to destruct the heap memory that i have created in copy constructor . So what can be done here ? please suggest

[EDIT]

delete should be used on memory allocated from heap using new only.

a1 = 10;
a = &a1;

In your case "a" is holding the address of memory in stack. So, you shouldn't call delete on that memory.

您必须删除以new (动态分配)获得的内存,而不是自动存储变量。

In copy constructor you are using new operator to assign the memory for pointer a ,so u can delete it.But since u are not using new operator in default constructor, you can delete the pointer a . You must use delete opearator to free the memory of variable which is in heap not in stack.

when you use new operator ,the variable is created in heap where as local variable are created in stack memory which can't be delete using delete operator

The problem is not the copy constructor, the problem is that you don't consistently use new (if you used move then maybe you would have a point). You have some options. I am trying to be useful, not exhaustive, maybe I'm missing other good design ideas.

Use new always (and consistently)

You can put a new in all the constructors, and thus, the destructor would always be able to call to delete . Even when you don't need it. It may be a ugly hack, but is consistent.

Never use new

If you never use new , you don't use delete either and the compiler ensures consistency and ensures that you don't mess up. Normally, you can rely on the compiler default behaviour on constructors.

Use smart pointers

If instead of calling to delete manually you save the variable inside a unique_ptr , then it will be deallocated at the destructor automatically by the compiler. The smart pointer is smart enough to know if it is initialized.

Beware that using a smart pointer means that you should never call delete , because if you do so without warning the smart pointer, you will stumble onto the same problem. If you are not familiarized with them and you are using C++11, it is a nice thing to learn :)

Track the status of your pointers

I would not vouch for this. It seems very C and not C++ . However, you can always track a bool value and at destroy time check it. This is just like using smart pointers, but with an in-house implementation. I think that it is a bad design idea, although educational in some cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM