#include <iostream>
usingnamespace std;
class myDouble//random class
{
friend ostream& operator << (ostream& output,myDouble &a)
{
output<<"a ="<<a.getA()<<" and b ="<<a.getB();
return output;
}
public :
myDouble(double b,int c):a(b),b(c)
{
cout<<"constructing"<<endl;
};
myDouble(const myDouble&a):a(a.a),b(a.b)
{
cout<<"This is a copy constructor and it is being used right now!\n";
}
~myDouble()
{
cout<<"destructing\n";
};
int getB()
{
return b;
}
void setB(int c)
{
b=c;
}
double getA()
{
return a;
}
void set(double b)
{
a=b;
}
private :
double a;
int b;
};
int main()
{
myDouble * myArrayptr[20];/*
Note that we are not using the new pointer.
So the memory is not supposed to be allocated right?
The memory is allocated whether we use new
or not.
*/
for(int i=0;i<20;++i)
myArrayptr[i]=NULL;//Writing 0 to all the pointers in the array
for(int i=0;i<20;++i)
{
cout<<"Size of myArrayptr["<<i<<"]="<<sizeof(*myArrayptr[i])<<endl;//The memory is allocated though...
}
myArrayptr[0]=new myDouble(0.0,1);//Creating 3 objects with new
myArrayptr[1]=new myDouble(1.0,2);
myArrayptr[3]=new myDouble(234.8,456);
myArrayptr[2]=new myDouble(*myArrayptr[0]);//initializing another element with the copy constructor
delete myArrayptr[3];//Deleting the object pointed by [3] the destructor is called
myArrayptr[0]=myArrayptr[3];/*
Assigning pointer [3] value to pointe [0] so the 0 should now point to the
object pointed by [3]
*/
for(int i=0;i<4;++i)
{
if(myArrayptr[i]!=NULL)
cout<<"myArrayptr["<<i<<"]="<<*myArrayptr[i]<<endl;
}
myArrayptr[3]=NULL;
//cout<<"myArrayptr["<<3<<"]="<<*myArrayptr[3]<<endl; This would cause error because pointer is pointing to 0
return 0;
}
The question is this :
After we use the delete operator the object pointed by that pointer is supposed to be destroyed right? The destructor is called so I suppose it's working. But how is it possible that I am able use the assignment operator to assign the "deleted" object to another dereferenced pointer? Furthermore how am I able to print it after I have deleted it? The final question is this :
Is the memory freed after I use the delete operator or not?
how is it possible that I am able use the assignment operator to assign the "deleted" object to another dereferenced pointer?
Because only the object the pointer was pointing to is destroyed. The pointer is still pointing to something. Remember that a pointer is nothing more than an integer interpreted a different way.
Furthermore how am I able to print it after I have deleted it?
The standard doesn't define what happens to the memory an object was using after the object no longer exists. Usually compilers leave the memory in the state it is after the destructor returned, but this is not necessarily true.
The standard also doesn't define what happens when an invalid pointer -- as is one that points to an object that no longer exists -- is dereferenced. Undefined behavior is very dangerous. When undefined behavior is triggered, the system can literally do anything. In most cases, it will just crash the program or let it pass, but you shouldn't count on that.