We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,186 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

deleting base pointer

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
3
Contributors
3
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
4
Views
Question
Answered
pushpat
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

Banfa
Practically a Master Poster
695 posts since Mar 2010
Reputation Points: 508
Solved Threads: 109
Skill Endorsements: 5

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;
}
nullptr
Junior Poster
154 posts since Mar 2012
Reputation Points: 46
Solved Threads: 32
Skill Endorsements: 0

Thanks a lot guys... :)

pushpat
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by Banfa and nullptr

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0680 seconds using 2.73MB