Two-D array delete/pointer error?

I'm unable to delete my 2-D runtime array for some odd reason? Whenever I try to apparently I get an invalid pointer error, and something about size error whenever I put in a value greater than or equal to 5 for either the col and rows and then try to delete them with the destructor. Thus once I got rid of deleting the array (which I know is not a good thing because its going to take memory) it worked fine. Soo yeah how can I delete my array within the destructor without getting this error?
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <climits>

using namespace std;
struct a1 {
private:
	int* x;
	int bytes;
	int go;
	int counter;
public:
	int& operator [] (int index) {
		return x[index];
	}
	a1(int a);
	~a1();
	void give() {
		for (int i = 0; i < go; i++) {
			cout << x[i];
		}
		cout << endl;
		cout << "Your array is " << bytes << "Bytes\n";
	}

};
a1::a1(int a) {
	cout << "Constructing the array ";
	x = new int[a];
	go = a;
	bytes = a * sizeof(x[0]);
	for (int i = 0; i < a; i++) {
		x[i] = 0;
	}
}
a1::~a1() {
	cout << "Destroying the array ";
	delete[] x;
}
struct a2 {
private:
	char **coL;
	char *row;
	char myC;
	int nC;
	int nR;
	int bytes;
	int sT;
public:
	a2(int ROW, int COL, char pCH);
	~a2();
	void getArray() {
                //Displays the array and sets the value as well.
		for (int z = 0; z < nC; z++) {
			sT++;
			for (int i = 0; i < nR; i++) {
				coL[z][i] = myC;
				cout << coL[z][i];
				if (i == (nR - 1)) {
					cout << endl;
				}
			}
		}
		cout << "Your array holds " << (bytes)<< "Bytes";
	}

};
a2::a2(int ROW, int COL, char pCH) {
       //Catches if user enters in 0 or a negative number for the two-D array.
	try {
		if (ROW < 0) {
			throw 1;
		}
		else if (COL < 0) {
			throw 2;
		}
		else if (COL < 0 && ROW < 0) {
			throw 3;
		}
		else if (COL == 0 || ROW == 0) {
			throw 4;
		}
		
	}
	catch (int x) {
		if (x == 1 || x == 2 || x == 3) {
			cout << "Cannot initialize an array with a negative value ";
		}
		else
			cout << "Arrays can not hold 0/NO values ";
	}
                //Sets the two-D array's values.
		sT = 0;
		nC = ROW;
		nR = COL;
		row = new char(ROW * COL);
		coL = new char*[ROW];
		myC = pCH;
		bytes = (sizeof(ROW) * (ROW * COL) / 4);
		for (int i = 0; i < ROW; ++i) {
			coL[i] = row + (COL * i);
		}
}

a2::~a2() {
	cout << "\tDestroying twoD array " << endl;
	//delete[] row;
	//delete[] coL;
}
int main() {
	char response;
	char myC;
	int pChoice;
	int a, b;
	bool stay = true;
	while (stay == true) {
		cout << "Would you like a 2-D array or 1-D array " << endl;
		cin >> pChoice;
		if (pChoice == 1) {
			cout << "How many values would you like your array to store? " << endl;
			cin >> a;
			b = a - 1;
			a1 k(a);
			k.give();
		}
		else if (pChoice == 2) {
			cout << "What kind of grid would you like? " << endl;
			cin >> a >> b;
			cout << "What kind of characters would you like your grid to be ";
			cin >> myC;
			cin.ignore(1000, '\n');
			a2 G(a, b, myC);
			G.getArray();
		}
		else {
			cout << "Not a valid choice" << endl;
			cin.clear();
			cin.ignore(1000, '\n');
			continue;
		}
	}
	cin >> response;
	return 0;
}
Last edited on
It's very simple. In the constructor of a2 you only allocate the memory (in the catch block) when the user enters 0 or negative number.
What is the point in throwing an exception when you catch it there as well?

BTW. If this sth. serious or do you just try a few things?
I got it and I just like trying to do some thing and learn things. I'm inexperienced with exceptions so where should I catch it once thrown?

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <climits>

using namespace std;
struct a1 {
private:
	int* x;
	int bytes;
	int go;
	int counter;
public:
	int& operator [] (int index) {
		return x[index];
	}
	a1(int a);
	~a1();
	void give() {
		for (int i = 0; i < go; i++) {
			cout << x[i];
		}
		cout << endl;
		cout << "Your array is " << bytes << "Bytes\n";
	}

};
a1::a1(int a) {
	cout << "Constructing the array ";
	x = new int[a];
	go = a;
	bytes = a * sizeof(x[0]);
	for (int i = 0; i < a; i++) {
		x[i] = 0;
	}
}
a1::~a1() {
	cout << "Destroying the array ";
	delete[] x;
}
struct a2 {
private:
	char** row;
	char* col;
	char myC;
	int nC;
	int nR;
	int bytes;
	int sT;
public:
	a2(int ROW, int COL, char pCH);
	~a2();
	void getArray() {
		for (int z = 0; z < nC; z++) {
			sT++;
			for (int i = 0; i < nR; i++) {
				row[z][i] = myC;
				cout << row[z][i];
				if (i == (nR - 1)) {
					cout << endl;
				}
			}
		}
		cout << "Your array holds " << (bytes)<< "Bytes";
	}

};
a2::a2(int ROW, int COL, char pCH) {
	try {
		if (ROW < 0) {
			throw 1;
		}
		else if (ROW < 0) {
			throw 2;
		}
		else if (ROW < 0 && COL < 0) {
			throw 3;
		}
		else if (COL == 0 || ROW == 0) {
			throw 4;
		}
		
	}
	catch (int x) {
		if (x == 1 || x == 2 || x == 3) {
			cout << "Cannot initialize an array with a negative value ";
		}
		else
			cout << "Arrays can not hold 0/NO values ";
	}
		sT = 0;
		nC = ROW;
		nR = COL;
		col = new char[ROW*COL];
                row = new char*[ROW];
                for (int i = 0; i < ROW; ++i)
    	 	row[i] = col + COL*i;
		myC = pCH;
		bytes = (sizeof(ROW) * (COL * ROW) / 4);
}

a2::~a2() {
	cout << "\tDestroying twoD array " << endl;
	delete[] row;
	delete[] col;
}
int main() {
	char response;
	char myC;
	int pChoice;
	int a, b;
	bool stay = true;
	while (stay == true) {
		cout << "Would you like a 2-D array or 1-D array " << endl;
		cin >> pChoice;
		if (pChoice == 1) {
			cout << "How many values would you like your array to store? " << endl;
			cin >> a;
			b = a - 1;
			a1 k(a);
			k.give();
		}
		else if (pChoice == 2) {
			cout << "What kind of grid would you like? " << endl;
			cin >> a >> b;
			cout << "What kind of characters would you like your grid to be ";
			cin >> myC;
			cin.ignore(1000, '\n');
			a2 G(a, b, myC);
			G.getArray();
		}
		else {
			cout << "Not a valid choice" << endl;
			cin.clear();
			cin.ignore(1000, '\n');
			continue;
		}
	}
	cin >> response;
	return 0;
}
Last edited on
It depends a bit on the application where you catch and handle an exception.
As a rule of thumb you catch an exception in the code that calls the exception throwing code code.
In this case it is main().
Topic archived. No new replies allowed.