I have a C++ class Matrix2 which contains a public method, foo()
, and a protected member, m
, declared here
public:
void foo();
protected:
float m[2][2];
Assume that the entire contents of the array m are zero initialized in the constructor.
Inside another function somewhere, I declare a Matrix2 on the stack.
Matrix2 MAT;
Then, in that same function, I call some function, foo, a public function in the Matrix2 class.
MAT.foo();
Inside foo(), I set
float ** u = (float **) m;
afterwards, within foo, I print out the following values.
cout << "m :: " << u << endl;
cout << "u :: " << m << endl;
cout << "&m[0][0] :: " << &(m[0][0]) << endl;
cout << "m[0][0] :: " << m[0][0] << endl;
cout << "u[0][0] :: " << u[0][0] << endl;
The results: u's mem address is the same as m's, as well as the same as &(m[0][0]).
Dereferencing m using m[0][0], the value prints out fine, as expected.
However, dereferencing u using u[0][0], the program crashes and is unable to read the memory, even though the pointer supposedly points to the same location.
Any ideas as to why this is?