2d pointer changes values involuntarily

i am fairly new to c++ and i am having a problem with a 2d array pointer:

i have a class Matrix-
2d array is supposed to keep values
derived class RotationMatrix creates a matrix based on args

here is the code (omitted lines)
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
//Matrix.cpp
//omitted includes
//many omitted constructors and functions
class Matrix
{
	/*the Matrix class creates matrices that can 
	be multiplied with others from a 2D array
	-dimensions rows,cols
	-matrix[r][c]*/
	public:
	//constructors
		Matrix();
	//display function-error here
                void display(void);
	//variables
		float **matrix;
		int rows,cols;
};
Matrix::Matrix()					//create an empty matrix with dimension(0,0)
{
	rows=0;cols=0;
}
void Matrix::display()				//diplays a matrix in a nice way
{                                             /*this function is causing a problem probly when it is called, however it works fine when called from the constructor*/
                                              /*however it works with the Matrix class but not with the RotationMatrix...*/
	cout.precision(3);
	cout << "[";
	for(int y=0; y<rows; y++)
	{
		if(y!=0)
			cout << ' ';
		cout << '[';
		for(int x=0; x<cols; x++)
		{
			cout.width(11);
			cout << matrix[y][x];    //this prints out random numbers... when called from outside
			if(x!=cols-1)
				cout << ',';
		}
		cout << ']';
		if(y!=rows-1)
			cout << '\n';
	}
	cout << "]\n";
}
class RotationMatrix: public Matrix
{
	/*creates a rotation matrix derived
	from the matrix class*/
	public:
	//constructors
		RotationMatrix(float, char);
};
RotationMatrix::RotationMatrix(float angle, char axis=0)	//creates a rotation matrix by x angle around a axis
{
	rows=3; cols=3;
	float temp[3][3];
        //OMITTED statements initializing matrix with arguments given
	//if...
        //else
        {
               //testing matrix
		temp=  {{1,4,7},
				{2,5,8},
				{3,6,9}};
	}
	matrix=new float*[3];
	for(int i=0;i<rows;i++)       //initializes pointer to array
	{
		matrix[i]=new float[cols];
		matrix[i]=temp[i];
	}
}

sample main program:
1
2
3
4
5
6
7
8
9
10
int main()
{
	RotationMatrix m(0,' ');
        cout << m.matrix[0][0] <<endl; //prints right value
	m.display();
        cout << m.matrix[0][0] << endl; //prints non-right value
	
	cout << endl;
	return 0;
}


here is the output:

1
[[      -1.14,   3.99e-34,   3.99e-34]
 [   1.54e-44,      -1.14,   5.34e-39]
 [   3.99e-34,          0,    2.8e-45]]
-1.14



i think the error occurs right when calling the display function
it might have to do with it creating some variables of some sort and messing with the 2d pointer

i usually program in java but i look forward to learning c++
any help from 'da pros' would be appreciated

please let me know if the code does not make much sense

btw im trying to make a 3d engine and this is my first class

thanks
closed account (D80DSL3A)
I see a problem in the RotationMatrix constructor. Lines 67 - 72:
1
2
3
4
5
6
matrix=new float*[3];
for(int i=0;i<rows;i++)       //initializes pointer to array
{
	matrix[i]=new float[cols];
	matrix[i]=temp[i];
}

You are allocating memory for the matrix OK but the assignment matrix[i]=temp[i]; is bad. You are assigning pointers here. Each matrix[i] is a pointer which you are assigning to point to the rows of the local array temp[3][3]. The temp array ceases to exist (goes out of scope) after the function exits.

You probably meant to copy the values from the temp array to the matrix array. Do this in a separate nested for loop after allocating memory.
1
2
3
for(int i=0;i<3;i++)
    for(int j=0;j<3;j++)
        matrix[i][j] = temp[i][j];   

Hopefully that's all that's wrong.
wow.
it worked the first time.
thank you so much, i really appreciate your help
it seems so obvious

is there another way to directly assign values to a pointer 2d array?
Topic archived. No new replies allowed.