1,105,644 Community Members

deleting base pointer

Member Avatar
pushpat
Newbie Poster
4 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi Folks :),

I need your help:?:, I am getting segmentation fault for "delete bptr_1" ( please refer program written below and line number 46 ).

If i remove statements "delete dptr;" (@line 32) inside try{}, then the program executes otherwise it will give error.

Could you please tell me why shouldnot i delete "dptr" inside try{}
Does delete dptr inside try{} deletes bptr_1 too?

#include<iostream>
using namespace std;

class base
{
	public:
	virtual ~base()
	{ }

};

class derived: public base
{
	public:
	~derived()
	{ }

};

int main()
{
	base *bptr_1 = new derived();
	base *bptr_2 = new base();
	derived *dptr;

	try
	{
		dptr = dynamic_cast<derived*>(bptr_1);
		if(dptr == 0)
			cout<<"(1) base ptr1 not pointing to derived"<<endl;

              delete dptr; // deleting memory allocated. If i remove this line,then program works
	      dptr = 0;  // assigning the pointer to null, to reuse

		dptr = dynamic_cast<derived*>(bptr_2);
		if(dptr == 0)
			cout<<"(2) bptr_2 not pointing to derived"<<endl;
	}
	catch (exception& err)
	{
		cout<<"error catched :"<<err.what()<<endl;
	}


	cout<<"delete base ptr pointing to d"<<endl;
	delete bptr_1;   //SEGFAULT HERE

	cout<<"delete base ptr pointing to base"<<endl;
	delete bptr_2;

	cout<<"delete derived ptr"<<endl;
	delete dptr;
	return 0;
}
OUTPUT:

(2) bptr_2 not pointing to derived
delete base ptr pointing to d
Segmentation fault
Member Avatar
Banfa
Practically a Posting Shark
801 posts since Mar 2010
Reputation Points: 582 [?]
Q&As Helped to Solve: 134 [?]
Skill Endorsements: 12 [?]
Featured
 
0
 

dptr and bptr_1 both point to the same allocated objected. You should only delete an object once, as soon as you delete it the pointer becaomes invalid and any access to that pointer, either dereferencing it or trying to delete it again is undefined behavior.

You allocate the object pointed to by dptr and bptr_1 at line 22 but then you try to delete it at lines 32, 46 and 52. This will always cause an error.

Only delete an object once.

Member Avatar
nullptr
Posting Whiz in Training
253 posts since Mar 2012
Reputation Points: 49 [?]
Q&As Helped to Solve: 49 [?]
Skill Endorsements: 2 [?]
 
0
 

This should work for you. :)

#include<iostream>

using namespace std;

class base
{
public:
    virtual ~base()
    { }

};

class derived: public base
{
public:
    ~derived()
    { }

};

int main()
{
    base *bptr_1 = new derived(); // should check that memory allocation was successful
    base *bptr_2 = new base();    // as above
    derived **dptr;               // double pointer

    try
    {
        dptr = reinterpret_cast<derived**>(&bptr_1);
        if(*dptr == NULL)
        {
            cout<<"(1) base ptr1 not pointing to derived"<<endl;
        }

        delete *dptr; // deleting memory allocated for bptr1.

        dptr = reinterpret_cast<derived**>(&bptr_2);
        if (*dptr == NULL)
        {
            cout<<"(2) bptr_2 not pointing to derived"<<endl;
        }

        cout<<"delete derived ptr"<<endl; // deleting memory allocated for bptr2.
        delete *dptr;
    }
    catch (exception& err)
    {
        cout<<"error caught :"<<err.what()<<endl;
    }

    system("Pause");

    return 0;
}
Member Avatar
pushpat
Newbie Poster
4 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks a lot guys... :)

Question Answered as of 2 Years Ago by Banfa and nullptr
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article