Using the infamous delete operator

Hi all. I am a beginner in C++ and I have this question.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>

using namespace 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.

And delete is hardly infamous.
Thanks for the answer helios. The infamous was ironic :)..However the memory is indeed deallocated right i.e it can be used for storing other stuff?
Topic archived. No new replies allowed.