0

I just made a class called "matrix4" which has a pointer to float, called data.

class matrix4
{
public:
    float * data;

The constructor, receives 16 floats, allocates space and inicializes every cell with the respective float:

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];
    }
}

Now I'm doing a factory class that produces matrix4. This specific one,receives a vector of scale (I've created the vector3D class too) and returns a scale matrix:

 matrix4 factory::createScaleMat4(vector3D v) {
    matrix4 m = matrix4(v.get_x(), 0, 0, 0,
                        0, v.get_y(), 0, 0,
                        0, 0, v.get_z(), 0,
                        0, 0, 0, 1);
    return m;
};

The problem is that it gives me this error: "Run-Time Check Failure #2 - Stack around the variable 'm' was corrupted", and I can't understand why...

Bernardo Bacalhau
  • 155
  • 1
  • 1
  • 8
  • 1
    Did you implement all 5 special functions **correctly**? – WhiZTiM Oct 18 '17 at 23:58
  • FTR: Your constructor can be improved. And you could have simply used a `std::vector` as `data`; – WhiZTiM Oct 18 '17 at 23:59
  • I can't use `std::vector` because my teacher doesnt allow. I just can't understand why is 'm' corrupted, if i'm initializing it correctly. – Bernardo Bacalhau Oct 19 '17 at 00:13
  • Do you have a copy constructor for `matrix4`? Does it make a copy of the `data` array? If the destructor deletes `data`, and you use the default copy constructor, the copy that's returned by `createScaleMat4` will have a pointer to the deleted array, resulting in undefined behavior when you use it. – Barmar Oct 19 '17 at 00:19
  • @BernardoBacalhau -- *(I've created the vector3D class too)* -- And if it has the same issues as your `matrix4` class, passing it by value as you're doing now here `createScaleMat4(vector3D v)` will lead to memory corruption. Did your teacher tell you about these very important things that persons already have mentioned, such as copy constructor, assignment operator, etc? – PaulMcKenzie Oct 19 '17 at 00:28
  • Here's my destructor: `matrix4::~matrix4() { delete data; }` – Bernardo Bacalhau Oct 19 '17 at 09:27
  • And my copy constructor: `matrix4::matrix4(matrix4& m) { data = new float[16]; for (int i = 0; i < 16; i++) data[i] = m.get_data(i); }` – Bernardo Bacalhau Oct 19 '17 at 09:28
  • @BernardoBacalhau -- Your code uses the wrong form of `delete[]`, and has no assignment operator for `matrix4`. In addition, your `vector3D` class better have correct copy semantics, as passing it by value will also cause undefined behavior (you are doing that now, and you didn't post that class). That `vector3D` is a likely candidate for contributing to what is causing your memory issues, and `matrix4` could be just a red-herring making you look in the wrong direction. This is why a [mcve] is always best, not code snippets. – PaulMcKenzie Oct 20 '17 at 01:04

0 Answers0