Proper implementation of a destructor for 2D matrix?

I am fairly new to C++ so this may be a simple question. I have created a dynamically allocated matrix using the following code:
1
2
3
4
5
data = new matrixData* [row_input];
// Create matrix with dimensions given
for (int i = 0; i < row_input; i++) {
    data[i] = new matrixData [column_input];
}


I get errors when I use the line ~matrix(); in the header file for the destructor with and the following code in the implementation file
1
2
3
4
5
6
matrix::~matrix()
{
    for (int i = 0; i < row; i++)
        delete[] data[i];
    delete[] data;
}


Without the destructor, I do not get any errors. The error in particular is not when I compile; ot is when I run. I get "Access violation reading location". It points me to a line in my code where I am trying to read a value from a passed by reference matrix. For example it points to the the 7th line in the following
1
2
3
4
5
6
7
8
9
10
11
12
matrix& matrix::operator=(const matrix& passed) 
{
    if(this != &passed) {
        // Create left handed matrix again with right handed dimensions
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < passed.column; j++) {
                data[i][j] = passed.data[i][j];
            }
        }
    }	
    return *this;
} // end operator= 
You have to post the rest of your matrix class and its header file.

At a minimum operator= needs to free the memory allocated by
the *this object since you are re-initializing it.
Thanks to both for your help but I still don't know how to fix my problem. I have simplified my code as much as possible and have it still give me the error. Here is my class, header, and main code. The code builds fine with no errors or warnings but I get severe problems when I run it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// HEADER FILE
#include <iostream>
#include <iomanip>
#include <stdlib.h>

using namespace std;

typedef int p2dArrayData;

class p2dArray
{	
	public:
		p2dArray();
		p2dArray (int rows, int cols);
		
		p2dArray operator+(const p2dArray&);
		p2dArray& operator=(const p2dArray&);

		~p2dArray();
	private:
		int HEIGHT;
		int WIDTH; 
		p2dArrayData **contents;
};


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
// Matrix Class
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include "p2dArray.h"
using namespace std;

p2dArray::p2dArray()
{
}

p2dArray::p2dArray(int ROWS_Given, int COLS_Given)
{
	HEIGHT = ROWS_Given;
	WIDTH = COLS_Given;
	contents = new p2dArrayData* [ROWS_Given];

	for (int i = 0; i < ROWS_Given; i++) {
		contents[i] = new p2dArrayData [COLS_Given];
	}

	for(int i = 0; i < ROWS_Given; i++) {
		for(int j = 0; j < COLS_Given; j++) {
			contents[i][j] = (p2dArrayData) i * j;
		}
	}
}

// Deep Copy Constructor
p2dArray& p2dArray::operator=(const p2dArray& operand) 
{
	if(this != &operand) {
		for(int i = 0; i < operand.HEIGHT; i++) {
			for(int j = 0; j < operand.WIDTH; j++) {
					contents[i][j] = operand.contents[i][j];
			}
		}
	}	
	return *this;
}

// Overloaded + Operator
p2dArray p2dArray::operator+(const p2dArray& operand)
{
	p2dArray temp2d(HEIGHT, WIDTH);
	if(HEIGHT != operand.HEIGHT && WIDTH != operand.WIDTH) throw(1);
	for(int i = 0; i < HEIGHT; i++) {
		for(int j = 0; j < WIDTH; j++) {
			temp2d.contents[i][j] = contents[i][j] + operand.contents[i][j];
		}
	}
	return temp2d;
} 

// Destructor
p2dArray::~p2dArray()
{
	for (int i = 0; i < HEIGHT; i++)
		delete[] contents[i];
	delete [] contents;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// MAIN FILE
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include "p2dArray.h"

using namespace std;

int main()
{
	p2dArray first(2, 3);
	p2dArray second(2, 3); 

	first = second + first;

	return 0;
}


My hypotheses about why I get the memory error is either in not implementing the deep copy constructor correctly or having a function "first = first + second" where it doesn't know which operation to do first. (Is there an inherent order of operations even for overloaded operators?). I posted this forum originally as a question of how to do the destructor because when I remove the lines for the destructor, my error goes away. It still works that way with the code I provided.

Have you tried a debugger?
Your default constructor does not initialize the data members (especially the pointer).
His code doesn't call the default constructor in this instance.
Yeah, that was my problem. Thanks!
Topic archived. No new replies allowed.