Deconstructor issue!

I don't think my deconstructor is working properly

the message in the deconstructor, to let the user know it worked properly, is not showing on my output, and I cannot for the life of me find where the problem lies


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
#include <iostream>

using namespace std;

class Myclass
{
private:
	int *ptr; //pointer to 1D dynamic array 
	int size;
public: 
	Myclass() //default constructor
	{
		ptr = NULL;
		size = 0;
	}
	Myclass(int arraySize) //overload constructor 
	{
		size = arraySize;
		ptr = new int[size];
		for (int i = 0; i < size; i++)
		{
			ptr[i] = 0;
			cout << ptr[i] << " ";
		}
		cout << endl;
	}
	Myclass(Myclass &x)
	{
		size = x.size;
		int *ptr = new int[size];

		cout << "Object now contains data: " << size << " size of array" << endl;
		cout << "Succesful copy!" << endl;
	}
	~Myclass()
	{
		delete [] ptr;
		*ptr = NULL;
		cout << "Memory reallocated!" << endl;
	}
};

int main()
{
	Myclass A;
	Myclass B(5); 
	Myclass C(B); //copy object B to C

	system("PAUSE");
	return 0;


}
I might be wrong about this but I do not think do not want to reallocate memory, I think your just initializing a pointer that does not exist in this case, inside of your destructor.

All that should be there are instructions of what to delete.
I would assume unexpected behavior with the way your code is written

Also look at your code in a debugger. That will tell you what is actually happening.
Last edited on
I don't think you are right.

I am not initializing anything in the destructor, i am trying to free up memory but deleting the previous objects array ptr

1
2
3
4
5
6
~Myclass()
	{
		delete [] ptr;
		*ptr = NULL;
		cout << "Memory reallocated!" << endl;
	}



So you delete a pointer.
Then set a pointer, that does not exist to null
Then cout your reallocating memory.

1
2
3
4
~Myclass()
	{
		delete ptr;
	}



I would just run the above code like so.

I have not tired to compile the above code, but I think you may be doing too much in your destructor. Just bring it back have it do one job. E.g deleting objects/freeing memory.
Last edited on
well ptr is an array

so it would have to be delete [] ptr;

I took off the *ptr = NULL;

but my cout message is still not being outputted, which means the deconstructor is not working properly and there is a memory leak
You might have to traverse your object and delete each allocation of your array.

e.g

create a
 
bool IsEmpty() 

method.


then in your destructor

1
2
3
4
5
6
7
8
~Myclass()
	{
                while(!IsEmpty())
               {
                    delete ptr; // removed the [] because you would be looking at each array member.
                }
		cout << "Memory reallocated!" << endl;
	}



you might be able to call your current object using


1
2
3
4
5
6
7
8
9
~Myclass()
	{
                while(this->!IsEmpty())
               {
                    delete ptr; // removed the [] because you would be looking at each array member. 
                }
		cout << "Memory reallocated!" << endl;
	}

Last edited on
I tried doing a condition similar to yours;
1
2
3
4
5
6
7
8
~Myclass()
	{
		if (ptr != NULL)
		{
			delete ptr;
			cout << "Memory reallocated!" << endl;
		}
	}


alas, the issue is still unresolved
closed account (D80DSL3A)
When main hits system("PAUSE"); the program hasn't ended yet, so the destructors won't have been called yet.
Look for the output to flash briefly as the window closes.

Try this: Write a simple function with a local MyClass object declared. You should see the dtor output when the function returns and the local MyClass is destroyed.
1
2
3
4
5
6
7
8
9
10
11
12
void testDtorOutput()
{
    MyClass test;
}

int main()
{
    testDtorOutput();

    system("PAUSE");
    return 0;
}


P.S. Your copy ctor isn't copying anything and it just throws away the block of memory allocated to the local variable int *ptr;
Try writing a displayContents() function then see what object C contains.
Last edited on
Sorry I am going back and forth on this while working on something that is due tonight.
I did not test any of my code that I gave you.

1
2
3
4
5
6
7
8
9
10
~Myclass()
	{
		if (ptr != NULL)
		{
			delete ptr; // you need to increment your pointer some how, to traverse your array
                                        // like you pointed out this would only be the front of your pointer array

			cout << "Memory reallocated!" << endl;
		}
	}
I don't understand, so why doesn't delete [] ptr work?

ptr is just a 1 dimensional dynamic array, doesnt delete [] simply delete all the contents of a 1D array?
1
2
3
4
5
6
7
8
int  *i;
int *ia;

i = new int; // only 1 int
ia = new int[5]; // array of 5 ints

delete i;    
delete [] i; // if you use new foo[xxx] you need to use delete [] foo 
Actually the stack takes care of itself. You only ever need to delete after a call to new. Think of the difference here:

1
2
int i[8]; // stack
int *i = new int[8]; // heap 


Topic archived. No new replies allowed.