簡體   English   中英

Visual Studio C ++ 0xC0000005錯誤:內存分配引起混淆

[英]Visual Studio C++ 0xC0000005 error: confused by 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 */

此類基於矩陣示例給出的示例

錯誤消息為“ 0xC0000005:訪問沖突讀取位置0x5820A694”。 所以我猜想內存分配是錯誤的,和/或數組超出范圍,和/或弄亂了'&'運算符。

編輯:我得到以下跟蹤:

  • 這個0x00dffe24 {values = {size = 9} rows = 3}矩陣*

因此,矩陣確實存在,但是由於某種原因我遇到了錯誤。

您正在使用的for循環,在引起錯誤的循環之前

for (auto& i : values)

是基於范圍的for循環。 這樣,您將獲得vector( values )中存在的values

但是根據您所寫的邏輯,這里需要一個索引來表示您正在處理的行。 您應該進行正常的for循環。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM