I've created a class called vector3D, which has x, y and z, and some functions to manipulate this vector:
class vector3D {
float x, y, z;
public:
vector3D();
vector3D(float);
vector3D(float, float, float);
vector3D(vector3D& v);
float get_x();
float get_y();
float get_z();
vector3D sum_vec_3D(vector3D);
};
vector3D::vector3D() {
x = 0;
y = 0;
z = 0;
}
vector3D::vector3D(float c) {
x = c;
y = c;
z = c;
}
vector3D::vector3D(float cx, float cy, float cz) {
x = cx;
y = cy;
z = cz;
}
vector3D::vector3D(vector3D& v) {
x = v.x;
y = v.y;
z = v.z;
}
vector3D vector3D::sum_vec_3D(vector3D b) {
return vector3D(x + b.x, y + b.y, z + b.z);
}
float vector3D::get_x() {
return x;
}
float vector3D::get_y() {
return y;
}
float vector3D::get_z() {
return z;
}
}
Then I've a class matrix4, that creates matrix 4x4. It has a pointer, and each time I create a matrix I allocate space (probably the error is in this class, but I don't know where):
class matrix4 {
public:
float * data;
public:
matrix4();
matrix4(float);
matrix4(float, float, float, float,
float, float, float, float,
float, float, float, float,
float, float, float, float);
matrix4(matrix4& m);
~matrix4();
};
matrix4::~matrix4()
{
delete [] data;
}
matrix4::matrix4() {
data = new float[16];
for (int i = 0; i < 16; i++) {
data[i] = 0;
}
}
matrix4::matrix4(float c) {
data = new float[16];
for (int i = 0; i < 16; i++) {
data[i] = c;
}
}
matrix4::matrix4(float c0, float c1, float c2, float c3, float c4, float c5, float c6, float c7, float c8, float c9, float c10, float c11, float c12, float c13, float c14, float c15) {
data = new float[16];
float input_vector[16] = { c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15 };
for (int i = 0; i < 16; i++) {
data[i] = input_vector[i];
}
}
matrix4::matrix4(matrix4& m) {
data = new float[16];
for (int i = 0; i < 16; i++)
data[i] = m.get_data(i);
}
Now I've created a class factory that produces special matrices 4x4. I've created this example to ilustrate the error:
class factory {
public:
matrix4 createSpecial(vector3D eye, vector3D center);
};
matrix4 factory::createSpecial(vector3D a, vector3D b) {
vector3D c = a.sum_vec_3D(b);
return matrix4(c.get_x(), 0, 0, 0,
0, c.get_y(), 0, 0,
0, 0, c.get_z(), 0,
0, 0, 0, 1);
};
This function receives two vectors, creates a third one, and then returns a matrix4 with the result values in the diagonal. When I run this function I get the error message:
Stack around the variable 'c' was corrupted
The problem is your "copy" constructor
matrix4(matrix4& m).The problem with it is that
return matrix4(...);creates a temporary object, and temporary object can't be bound to non-constant references. In other words, your "copy" constructor is not called, leading to multiple objects with the samedatapointer.The solution is to create a proper copy constructor
matrix4(const matrix4& m).Or, as mentioned in my comment, don't do manual memory management at all and use the rule of zero where you don't need a copy constructor or a destructor.