c++ - Visual Studio C++ 0xC0000005 错误 : confused by memory allocation

标签 c++ visual-studio matrix dereference static-memory-allocation

我是一名 Java 开发人员,但现在我需要一个 C++ 库,而且我对这门语言的经验还不够丰富。特别是,我总是对指针、引用和内存分配感到困惑。我认为这就是我在开发的矩阵类中出错的原因。

主要代码:

#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 有趣的代码:

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

完整的 matrix.hpp 文件:

#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 */

此类基于 matrix example 中给出的示例.

错误显示“0xC0000005:访问冲突读取位置 0x5820A694。” 所以我猜内存分配是错误的和/或数组越界和/或我在搞乱“&”运算符。

编辑:我得到以下痕迹:

  • 这个 0x00dffe24 {values={ size=9 } rows=3 } 矩阵 *

所以,矩阵确实存在,但出于某种原因我得到了错误。

最佳答案

您正在使用的 for 循环,在导致错误的循环之前

for (auto& i : values)

是一个基于范围的 for 循环。 有了这个,您将获得 vector(values) 中存在的值。

但是根据您编写的逻辑,您在这里想要的是一个代表您正在使用的行的索引。 您应该使用普通的 for 循环。

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

关于c++ - Visual Studio C++ 0xC0000005 错误 : confused by memory allocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48418929/

相关文章:

C++ - 分发与开发不同的头文件

visual-studio - 为什么尝试制作脱机安装程序时 Visual Studio 2017 安装程序会退出

iOS - 使用 Accelerate.framework 计算矩阵每一行和/或列的点积

r - 在 R 中访问矩阵元素的另一种方法

c++ - 为什么 C++ 中的析构函数以与初始化相反的顺序释放内存?

c++ - 动态数组输出问题

c# - WPF:如何在代码中设置段落的内容?

c++ - 是否可以从 COFF 库文件 (.lib) 中去除调试信息?

arrays - 在按行排序的矩阵中找到表示最小整数的行

c++ - 滥用 istream& get