delete operator with multidimensional array (not 2D vector)

I'm having problem when trying to delete the object from main function.

My matrix class
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
class Matrix {

private:
    int **matrix;
    int nRow;
    int nCol;

public:
    Matrix(const int nRow, const int nCol);
    void setElement(const int i, const int j, const int e);
    ~Matrix();
};

Matrix::Matrix(const int nRow, const int nCol)
{
    this->nRow = nRow;    this->nCol = nCol;

    matrix = new int * [nRow];
    for(int i = 0; i < nRow; i++) matrix[i] = new int [nCol];
}

void Matrix::setElement(const int i, const int j, const int e)
{
    matrix[i][j] = e;
}

Matrix::~Matrix()
{
    for(int i = 0; i < nRow; i++)
    {
        delete [] matrix[i];
        std::cout<<"Deleting row #" << i << '\n';
    }

    delete matrix;
    std::cout<<"Deleting matrix\n";
    matrix = 0;
}


The problem is when I use delete tab; in main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Matrix.h"

int main(int argc, char** argv) {

    Matrix tab(2,2);

    tab.setElement(0,0,1); tab.setElement(0,1,2);
    tab.setElement(1,0,3); tab.setElement(1,1,4);
    tab.print_screen();

    delete tab;

    return (EXIT_SUCCESS);
}


How can I delete my object using delete operator?

Thanks in advance
I'm not sure but
1
2
3
4
for(int i = 0; i < nRow; i++)
        delete [] matrix[i];
//    delete matrix; You're just deleting the first cell of the array
     delete [] matrix;


If you want to use delete, need to overload the operator
void operator delete(void *) //expect a pointer so you call delete &tab

To destroy an object, call explicit at the destructor tab.~Matrix() (it will be called automatically at the end of scope)
Thanks ne555.

The destructor will be always called at the end of scope?

I called explicit the destructor with tab.~Matrix(), but at the end it was called again automaticaly.
You don't delete something that was not created with new. I fixed one place where you used delete rather than delete[]

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

class Matrix
{

private:
	int **matrix;
	int nRow;
	int nCol;

public:
	Matrix(const int nRow, const int nCol);
	void setElement(const int i, const int j, const int e);
	~Matrix();
};

Matrix::Matrix(const int nRow, const int nCol)
{
	this->nRow = nRow;
	this->nCol = nCol;

	matrix = new int *[nRow];
	for(int i = 0; i < nRow; i++)
		matrix[i] = new int[nCol];
}

void Matrix::setElement(const int i, const int j, const int e)
{
	matrix[i][j] = e;
}

Matrix::~Matrix()
{
	for(int i = 0; i < nRow; i++)
	{
		delete[] matrix[i];
		std::cout << "Deleting row #" << i << '\n';
	}

//	delete matrix;
	delete[] matrix; // this was alocated using []
	std::cout << "Deleting matrix\n";
	matrix = 0;
}

int main(int argc, char** argv)
{

	Matrix tab(2, 2);

	tab.setElement(0, 0, 1);
	tab.setElement(0, 1, 2);
	tab.setElement(1, 0, 3);
	tab.setElement(1, 1, 4);
	tab.print_screen();

	// No need to delete this, it was not created using new.
	//delete tab;

	return (EXIT_SUCCESS);
}
Thanks... I see the light now! I was forgetting that has "new" only inside the class and I was trying do delete outside.
Topic archived. No new replies allowed.