Delete dynamic multi-dimensional array

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
=
#include <iostream>
using namespace std;

typedef double* DoubleArrayPtr;

class TwoD
{
public:
	TwoD();
	TwoD(int,int);
	TwoD(const TwoD& copyMe);//This is a copy constructor,problem ?
	TwoD& operator =(const TwoD& rightSide);//Overloaded !
	~TwoD();//Destructor,xD,how I love this name
	const void setEntry(int theRow,int theCol);
	const double returnEntry(int theRow,int theCol) const;
	friend const TwoD operator +(const TwoD& anArray1,const TwoD* anArray2);
private:
	double *a;
	int maxRows;
	int maxCols;
};

int main()
{/*
	int d1,d2;
	cout << "Row -> Column for teh array \n";
	cin >> d1 >> d2;

	IntArrayPtr *m = new IntArrayPtr[d1];
	int i,j;
	for(i = 0;i<d1;i++)
		m[i] = new int[d2];

	cout << "Enter " << d1 << " rows of " << d2 << " intergers each ";
	for(i=0;i<d1;i++)
		for(j=0;j<d2;j++)
			cin >> m[i][j];

	cout << "Echo the 2 dim array :";
	for(i=0;i<d1;i++)
	{
		for(j=0;j<d2;j++)
			cout << m[i][j] << " ";
		cout << endl;
	}

	for(i=0;i<d1;i++)
		delete [] m[i];
	delete [] m;
	*/
	return 0;
}

TwoD::TwoD():maxRows(5),maxCols(5)
{
	double* *a = new double*[maxRows];
	for (int i = 0; i < maxRows; i++)
		a[i] = new double[maxCols];
}

TwoD::TwoD(int no1,int no2):maxRows(no1),maxCols(no2)
{
	double* *a = new double*[maxRows];
	for (int i = 0; i < maxRows; i++)
		a[i] = new double[maxCols];
}

TwoD::~TwoD()
{
	for(int i=0;i<maxRows;i++)
			delete [] a[i];
	delete [] a;
}

TwoD::TwoD(const TwoD& copyMe):maxRows(copyMe.maxRows),maxCols(copyMe.maxCols)
{
	a = new double[maxRows];

	for(int i=0;i<maxRows;i++)
		a[i] = new double[maxCols];

	for(int i=0;i<maxRows;i++)
		for(int j=0;j<maxCols;j++)
			a[i][j] = copyMe.a[i][j];
}

TwoD& TwoD::operator =(const TwoD& rightSide)
{
	if(maxRows != rightSide.maxRows || maxCols != rightSide.maxCols)
	{
		for(int i=0;i<maxRows;i++)
			for(int j=0;j<maxCols;j++)
				delete [] a[i];
		delete [] a;

		a = new double[maxRows];

		for(int i=0;i<maxRows;i++)
		a[i] = new double[maxCols];
	}
}

const void setEntry(int theRow,int theCol)
{
	double value;
	cout << "maxrow = " << maxRows << " maxcol = " << maxCols << endl;
	cin >> value;
	a[theRow][theCol]=value;
}


Look at the delete,please tell me what I did wrong because I get :

error C2541: 'delete' : cannot delete objects that are not pointers
Last edited on
you did wrong at:

delete [] a[i];

since a[i] is a double.
- 'a' in your class should be double** a;, not double* a;

- You also should not be redeclaring 'a' in your constructors. By doing so you are assigning a local variable (which leaks) and not assigning your member.

- Your copy constructor and assignment operator should be assigning a = new double*[x]
Last edited on
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
using namespace std;

typedef double* DoubleArrayPtr;

class TwoD
{
public:
	TwoD();
	TwoD(int,int);
	TwoD(const TwoD& copyMe);//This is a copy constructor,problem ?
	TwoD& operator =(const TwoD& rightSide);//Overloaded !
	~TwoD();//Destructor,xD,how I love this name
	const void setEntry(int theRow,int theCol);
	const double returnEntry(int theRow,int theCol) const;
	friend const TwoD operator +(const TwoD& anArray1,const TwoD* anArray2);
private:
	double** a;
	int maxRows;
	int maxCols;
};

int main()
{/*
	int d1,d2;
	cout << "Row -> Column for teh array \n";
	cin >> d1 >> d2;

	IntArrayPtr *m = new IntArrayPtr[d1];
	int i,j;
	for(i = 0;i<d1;i++)
		m[i] = new int[d2];

	cout << "Enter " << d1 << " rows of " << d2 << " intergers each ";
	for(i=0;i<d1;i++)
		for(j=0;j<d2;j++)
			cin >> m[i][j];

	cout << "Echo the 2 dim array :";
	for(i=0;i<d1;i++)
	{
		for(j=0;j<d2;j++)
			cout << m[i][j] << " ";
		cout << endl;
	}

	for(i=0;i<d1;i++)
		delete [] m[i];
	delete [] m;
	*/
	return 0;
}

TwoD::TwoD():maxRows(5),maxCols(5)
{
	a = new double*[maxRows];
	for (int i = 0; i < maxRows; i++)
		a[i] = new double*[maxCols];
}

TwoD::TwoD(int no1,int no2):maxRows(no1),maxCols(no2)
{
	a = new double*[maxRows];
	for (int i = 0; i < maxRows; i++)
		a[i] = new double*[maxCols];
}

TwoD::~TwoD()
{
	for(int i=0;i<maxRows;i++)
			delete [] a[i];
	delete [] a;
}

TwoD::TwoD(const TwoD& copyMe):maxRows(copyMe.maxRows),maxCols(copyMe.maxCols)
{
	a = new double*[maxRows];

	for(int i=0;i<maxRows;i++)
		a[i] = new double*[maxCols];

	for(int i=0;i<maxRows;i++)
		for(int j=0;j<maxCols;j++)
			a[i][j] = copyMe.a[i][j];
}

TwoD& TwoD::operator =(const TwoD& rightSide)
{
	if(maxRows != rightSide.maxRows || maxCols != rightSide.maxCols)
	{
		for(int i=0;i<maxRows;i++)
			for(int j=0;j<maxCols;j++)
				delete [] a[i];
		delete [] a;

		a = new double[maxRows];

		for(int i=0;i<maxRows;i++)
		a[i] = new double[maxCols];
	}
}

const void TwoD::setEntry(int theRow,int theCol)
{
	double value;
	cout << "maxrow = " << maxRows << " maxcol = " << maxCols << endl;
	cin >> value;
	a[theRow][theCol]=value;
}


1>c:\users\aspire\documents\visual studio 2010\projects\test2\test2\test.cpp(59): error C2440: '=' : cannot convert from 'double **' to 'double *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\aspire\documents\visual studio 2010\projects\test2\test2\test.cpp(66): error C2440: '=' : cannot convert from 'double **' to 'double *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\aspire\documents\visual studio 2010\projects\test2\test2\test.cpp(81): error C2440: '=' : cannot convert from 'double **' to 'double *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\aspire\documents\visual studio 2010\projects\test2\test2\test.cpp(97): error C2440: '=' : cannot convert from 'double *' to 'double **'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


Still fail...I don't know...
Last edited on
The fix for error lines 59, 66, 81 is:

a[i] = new double[maxCols];

The fix for error line 97 is:

a = new double*[maxRows];

Thank you all,nearly done,but I don't understand what is this :

double** a;

Why there are 2 asterisks ?
it means a is a pointer pointing to a pointer of double.

It's something like:

double * ( double *) a;

so it's shortened as "double**".

Then further more, you can use as many * as you like, such as "double ***** a"
So we must use

double** a;

and not

double* a;

in this problem ?

Thank you for your help.
In your current design, yes, you must use double**.

But you can also use double * with another design. Briefly, it's something like the snippet of code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
double * a;
int maxRows;
int maxCols;

a = new double[maxRows * maxCols];
delete [] a;

const void TwoD::setEntry(int theRow,int theCol)
{
	a[theRow * maxRows + theCol]=value;
}

const double returnEntry(int theRow,int theCol)
{
	return a[theRow * maxRows + theCol];
}

1
2
3
4
5
6
7
8
9
10
11
12
13
const void TwoD::setEntry(int theRow,int theCol)
{
	double value;
	cout << "row = " << theRow << " col = " << theCol << endl;
	cout << "Enter your wanted value";
	cin >> value;
	a[theRow][theCol]=value;
}

const double TwoD::viewEntry(int theRow,int theCol) const
{
	return a[theRow][theCol];
}


thanks
now about these 2 function,it's not the way the call the array like this,isn't it ?

EDIT : OK now,I forgot about the index 0 and get the heap error.
Last edited on
Topic archived. No new replies allowed.