Hi,
while executing below programme on my Dev-c++ 4.9.9.2 compiler , i was bit amazed as it should have not run properly due to use of deleted object (obj). can anybody let me know instead of crashing , it is getting executed without any memory issue.
~Mohan

#include<iostream.h>
#include<conio.h>
//virtual constructor 
using namespace std;

class Base { public: virtual ~Base() {}
void virtual show(){cout<<"Base";}
virtual Base * create( ) const=0; 

 };


class Derived1 : public Base
{
public:

   Derived1* create() const { return new Derived1; }
   void show(){cout<<"Derived1";}
};

class Derived2 : public Base
{
public:

   Derived2 * create() const { return new Derived2; }
   void show(){cout<<"Derived2";}
};



int main()
{
 Derived1 obj1;  
 Derived2 obj2; 
 Base * obj = obj1.create();
 obj->show();
 delete obj;
 obj=obj2.create();
 obj->show();
delete obj;
 getch();
return 1;    
}

I don't see any object being used after it is deleted. There are only two deletions. After the first one the pointer is immediately overwritten, and the second one is practically at the end of the program. Could you provide the line number where a deleted object may be used?

Edited 3 Years Ago by bguild

delete obj;
obj=obj2.create();
obj->show();
delete obj;

This is legal. Even though you deleted the obj (freed that memory), the pointer is overwritten by the next line obj = obj2.create();. So by calling obj->show(); after that, it would perfectly display "Derived2", since obj points to a new Object of type Derived2.

yeah..even without deleting the object it also works as the pointer is overwritten by
new memory block allocated by object type Derived2.

Base * obj = obj1.create();
 obj->show();
 obj=obj2.create();
 obj->show();
 delete obj;

Thanks Andrew for helping regarding this doubt

while executing below code, i found segmentation fault but not sure why it is happening
even memory allcation is done as per the size of object. Kindly suggest.

#include<iostream.h>

#include<conio.h>

#include<string.h>

using namespace std;



class Derived1

{

char *s;

int m_length;

public:

Derived1(const char*);

Derived1( const Derived1& obj);

void show()const  {cout<<"str="<<s;}



~Derived1(){



delete [] s;}

};



Derived1::Derived1(  const char *str="")

{   



         m_length=strlen(str)+1;//null terminator

         cout<<m_length;

         s=new char[m_length];

         strncpy(s,str,m_length);

         s[m_length-1]='\0';//as loop start from 0





 }



Derived1::Derived1( const Derived1 &obj)

       {

        if(obj.m_length)

        {

        cout<<obj.m_length;      

        s=new char [m_length];//crash

        strncpy(s,obj.s,m_length);

        s[m_length-1]='\0';

        } 



        else

        {

         s= new char[1];

         s='\0';



        }           



       }



int main()

{

  Derived1 s1;  

  Derived1 s2("mohan");

  s2.show();

  Derived1 s3=s2;

cout<<"s2.show";

s3.show();

getch();



}
This question has already been answered. Start a new discussion instead.