【问题标题】:Visual Studio C++ 0xC0000005 error: confused by memory allocationVisual Studio C++ 0xC0000005 错误:被内存分配混淆
【发布时间】:2018-01-24 09:28:03
【问题描述】:

我是一名 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 } 矩阵 *

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

【问题讨论】:

  • matrix构造函数中你已经初始化了初始化列表中的成员,你不需要构造函数体中的赋值。我也认为不需要复制构造函数,即复制赋值运算符的析构函数,您可以只使用编译器默认值。
  • 至于你的问题,我没有看到任何内存分配或指针(this 除外)?请learn how to debug your programs。更具体地说,学习如何使用调试器来捕获崩溃,并在代码中找到它发生的位置。
  • 调试并查看调用堆栈
  • #include "matrix.cpp" 之后我会停止阅读这段代码>。

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


【解决方案1】:

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

for (auto& i : values)

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

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

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

【讨论】:

    猜你喜欢
    • 2012-12-13
    • 1970-01-01
    • 2013-06-09
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    相关资源
    最近更新 更多