2D arrays (dynamcally alocated, with clases, operator overload)

Hi there. I'm strugling to learn C++, and some concepts seem to rub a bit harder on me than others.

At the moment I'm making a program which adds 2 dynamically created 2D arrays, and I'm taking it step by step.

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
 #include <iostream>
#include <new>
using namespace std;

class matrix {
public:	int **matrice;
	int n, i, j, rand, col;

	matrix(int, int);	
};

matrix::matrix(int rand, int col) {
	matrice = new int*[rand];
	for (i = 0; i < rand; i++)
		matrice[rand] = new int[col]; 

	for (i = 0; i < rand; i++)
		for (j = 0; j < col; j++)
		{
			cout << "randul " << i + 1 << " coloana " << j + 1 << " = ";
			cin >> *(*(matrice + i) + j);
		}
}

int main()
{ 
	matrix a(2, 2);
        return 0;
}


When I run it, I get some exceptions after I read the first value of the array. An "Access violation writing location 0xCDCDCDCD". And I have no ideea why...
Last edited on
Use std::vector to create dynamic arrays.

It's simple:

Define a new empty matrix:

vector< vector<int> > matrix;

Make a matrix of given size:

matrix = vector< vector<int> >(rows,vector<int>(columns));
Last edited on
Would it still be using dynamic alocated memory?
Yes, std::vector uses dynamically allocated memory. It will automatically 'resize' it if you change the size of a vector.
Thank you, I will try this method also.

And I also found the mistake in my code... a silly one.

1
2
for (i = 0; i < rand; i++)
		matrice[i] = new int[col]; 


instead of

1
2
for (i = 0; i < rand; i++)
		matrice[rand] = new int[col]; 


I'll contiune my program and post my progress.
I just added a simple function to my class to print the arrays I read. But it seems that the arrays disappear after the constructor done its job.
This is the function:
1
2
3
4
5
6
void matrix::print_m() {
	for (i = 0; i < rand; i++)
	{
		for (j = 0; j < col; j++)
			cout << matrice[i][j] << ' ';
		cout << endl;


And if I do this:
1
2
matrix a(2,2);
a.print_m();

after i fill the array, nothing happens; it doesnt get printed.Any ideeas why?
Last edited on
Managed to fix that too. It was because rand and col were not actually initialized anywhere. Onward to the operator overloading now!!!
Alas, my program is done. It has a minor inconvenience but it works.

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
#include <iostream>
#include <new>
using namespace std;

class matrix {
public:	int **matrice;
	int  i, j, rand, col;

	matrix();
	matrix(int, int);
	void print_m();
	matrix  operator + (const matrix&);	
};

matrix::matrix() {
	cout << "how many rand/cols : "; cin >> rand >> col;
	matrice = new int*[rand];
	for (i = 0; i < rand; i++)
		matrice[i] = new int[col];
}

matrix::matrix(int rand, int col) {
	this->rand=rand; this->col=col;
	matrice = new int*[rand];
	for (i = 0; i < rand; i++)
		matrice[i] = new int[col]; 

	for (i = 0; i < rand; i++)
	{
		for (j = 0; j < col; j++)
		{
			cout << "randul " << i + 1 << " coloana " << j + 1 << " = ";
			cin >> *(*(matrice + i) + j);
		}
		cout << endl;
	}
}

matrix matrix::operator + (const matrix& other)
{
	matrix result;
	for (i = 0; i < rand; i++)
		for (j = 0; j < col;j++)
		result.matrice[i][j] = matrice[i][j] + other.matrice[i][j];
		return result;
}

void matrix::print_m() {
	for (i = 0; i < rand; i++)
	{
		for (j = 0; j < col; j++)
			cout << matrice[i][j] << ' ';
		cout << endl;
	}
}

int main()
{
	matrix a(3,3),b(3,3),c;
	c = a + b;
	c.print_m();
}

The inconvenience is that the default constructor
1
2
3
4
5
6
matrix::matrix() {
	cout << "how many rand/cols : "; cin >> rand >> col;
	matrice = new int*[rand];
	for (i = 0; i < rand; i++)
		matrice[i] = new int[col];
}
is ran twice and you must write rand and col twice. But it does what its supposed to do.
Topic archived. No new replies allowed.