Search code examples
c++visual-studiomatrixdereferencestatic-memory-allocation

Visual Studio C++ 0xC0000005 error: confused by memory allocation


I am a Java developer, but now I need a C++ library and I am not so experienced in this language. In particular, I always get confused about pointers, references and memory allocation. This I think is the reason why I am getting an error at a matrix class I am developing.

The main code:

#include "stdafx.h"
#include "matrix.cpp"

void matrixtest();

int main()
{
    matrixtest();
    system("pause");
    return 0;
}

void matrixtest()
{
    // let's try 3x3 matrices
    static const int arr1[] = {1, 2, 1, -1, 1, 2, 2, 3, -4};
    static const int arr2[] = {0, 2, 2, 1, -1, 0, 3, 2, -2};

    vector<int> values1(arr1, arr1 + sizeof(arr1) / sizeof(arr1[0]));
    vector<int> values2(arr2, arr2 + sizeof(arr2) / sizeof(arr2[0]));

    matrix A(values1, 3);
    matrix B(values2, 3);

    matrix sum = A + B;
    sum.show();

    matrix diff = A - B;
    diff.show();

    matrix prod = A * B;
    prod.show();
}

matrix.cpp interesting code:

matrix::matrix(vector<int> v, int r) : values(v), rows(r) {
    values = v;
    rows = r;
}

// [...]

matrix& matrix::operator += (const matrix& rhs) {
    matrix result = (*this) + rhs;
    (*this) = result;
    return *this;
}

matrix matrix::operator + (const matrix& rhs) {
    if (rows != rhs.rows || values.size() != rhs.values.size()) {
        throw std::length_error("Matrices shapes mismatch");
    }
    matrix result(values, rows);
    for (auto& i : values) {
        result.values[i] = this->values[i] + rhs.values[i];
    }
    return result;
}

// [...]

void matrix::show() {
    string delimiter = "";
    for (auto& i : values) {
        delimiter = "";
        for (auto j = 0; j < values.size()/rows; j++) {
            cout << delimiter << values[i * values.size()/rows + j];  // this is the line giving the error
            delimiter = ",";
        }
        std::cout << std::endl;
    }
}

full matrix.hpp file:

#ifndef matrix_hpp
#define matrix_hpp

class matrix {
    private:
        std::vector<int> values;        // size is found by values.size()
        int rows;                       // columns is values.size()/rows

    public:
        matrix(vector<int>, int); // base ctor.
        matrix(const matrix& rhs); // copy ctor.
        matrix& operator=(const matrix& rhs); // assign. ctor.
        ~matrix(); // dtor.
        int& operator () (int row, int column);
        const int& operator () (int row, int column) const;     
        matrix operator + (int scalar) const;
        matrix operator - (int scalar) const;
        matrix operator * (int scalar) const;
        matrix& operator += (int scalar);
        matrix& operator -= (int scalar);
        matrix& operator *= (int scalar);

        matrix operator + (const matrix&);
        matrix operator - (const matrix&);
        matrix operator * (const matrix&);
        matrix& operator += (const matrix&);
        matrix& operator *= (const matrix&);

        // should be private ??
        void reshape(int newRows, int newColumns);
        void show(); //used for dev. only
        void range(int start, int defaultStep = 1);
        void fill(int value);
        void randint(int lowerBound, int upperBound);
};

#endif /* CMatrix_hpp */

This class is based on an example given at matrix example.

The error says '0xC0000005: Access violation reading location 0x5820A694.' So I am guessing the memory allocation is wrong and/or there is an out of bounds array and/or I am messing with the '&' operators.

Edit: I get the following trace:

  • this 0x00dffe24 {values={ size=9 } rows=3 } matrix *

So, the matrix does exist, but for some reason I am getting the error.


Solution

  • The for loop that you are using, before the one causing the error

    for (auto& i : values)
    

    is a range-based for loop. With this you will get the values present in vector(values).

    But based on the logic you have written what you want here is an index that represents the row you are working with. You should go for the normal for loop.

    for(int i =0; i<rows; ++i)