【问题标题】:file.exe error when compiling dynamic matrix编译动态矩阵时出现file.exe错误
【发布时间】:2015-04-30 12:46:49
【问题描述】:

我正在尝试使用类制作动态矩阵。我制作了这段代码。检查了其他代码,它似乎是正确的。编译时我没有收到任何错误消息,除了运行这部分代码时程序中间的 file.exe 错误。它说 file.exe 已停止工作。

有人能看出这段代码有什么问题吗?

头文件

#pragma once
#include <iostream>
class Matrix {
    unsigned int nRows, nColumns;
    double **board;
public:
    Matrix();
    Matrix(unsigned int nRows, unsigned int nColumns);
    ~Matrix();
};

.cpp

#include <iostream>
#include "matrix.h"
using namespace std;

Matrix::Matrix()
: nRows(0)
, nColumns(0)
, board(nullptr) 
{}
Matrix::Matrix(unsigned int nRows, unsigned int nColumns)
: nRows(nRows)
, nColumns(nColumns)
, board(nullptr)
{   

    board = new double*[nRows];
    for (int i = 0; i < nRows; ++i)
    {
    board[i] = new double[nColumns];
    }


    for (int i =0; i<nRows; ++i)
    {
        for(int j=0; j<nColumns; ++j)
        {
            board[i][j]=0.0;
        }
    }
}
Matrix::~Matrix() 
{
  for (int i = 0; i < nRows; ++i){
  delete [] board[i];
  delete [] board;
  board = 0;

  }
}

主要

#include <iostream>
#include "matrix.h"

int main(){
Matrix A = Matrix(5,5);
}

我正在运行 Visual Studios 2010

编辑: 我已经调试过,问题似乎出在默认构造函数和我尝试使用的构造函数中。在 .cpp 的第 13 行和第 17 行,它尝试访问此行: retval = HeapFree(_crtheap, 0, pBlock); 在 free.c 中

调试器状态 : 加载 'C:\Windows\SysWOW64\ntdll.dll', 找不到或打开 PDB 文件 0xC0000005 :访问冲突读取位置0x00000004

【问题讨论】:

  • 您提供的代码不是"Minimal, Complete, Verifiable Example",您甚至没有识别出哪一行崩溃。您需要提供适量的代码来重现问题,而无需额外包含任何内容。这是否与它在 Matrix 类中有关?你能写一个初始化板子的主程序并得到同样的问题,还是只有在你上课时才会出现问题?
  • 在Visual Studio中逐行调试这段代码,找出是哪一行导致它崩溃。
  • 我对编程和 stackoverflow 都很陌生,所以谢谢你让我知道。我现在已经隔离了程序,并给出了所有必要的代码。我必须开始工作,但我会在回家的第一件事上尝试逐行调试代码,也许这会提供一个线索。无论如何,感谢您抽出时间。
  • “它说 file.exe 已停止工作” 那是你加载调试器伴侣的时候
  • 我已经调试过了,问题似乎出在默认构造函数和我尝试使用的构造函数中。在 .cpp 中的第 13 行和第 17 行,它尝试访问此行: retval = HeapFree(_crtheap, 0, pBlock);在 free.c 调试器状态中:加载 'C:\Windows\SysWOW64\ntdll.dll',找不到或打开 PDB 文件 0xC0000005:访问冲突读取位置 0x00000004。

标签: c++ class dynamic matrix


【解决方案1】:

您正在制作 Matrix 的副本,但您没有有效的复制构造函数。将您的代码更改为以下内容:

#include <iostream>
#include "matrix.h"

int main(){
   Matrix A(5,5);
   return 0;
}

或者,您可以添加一个复制构造函数:

Matrix::Matrix(const Matrix& other) : 
    Matrix(other.nRows, other.nColumns)
{
    for (int i =0; i<nRows; ++i)
    {
        for(int j=0; j<nColumns; ++j)
        {
            board[i][j]=other.board[i][j];
        }
    }
}

不过,您采用的方法非常像“C”。如果我正在实现一个矩阵类,我会使用标准的 c++ 容器,可能是数组或向量,具体取决于具体情况。

这是一个矩阵的工作实现。我并没有真正考虑过或测试过,网上可能还有一百万个更好的实现:

#include <vector>
#include <iostream>
#include <stdexcept>
#include <iomanip>

using namespace std;

class Matrix {
public:
   Matrix() : nRows(0), nColumns(0), maxSize(0) {}
   Matrix(size_t r, size_t c) : 
      nRows(r), nColumns(c), cont(r*c, 0), maxSize(r*c)
   {      
   }

   Matrix(const Matrix& other) : 
      nRows(other.nRows), 
      nColumns(other.nColumns),
      cont(other.cont.begin(), other.cont.end())
   {
   }

   void grow(size_t r, size_t c) {
      vector<double> growth(r*c, 0);
      for (size_t i=0; i<nRows; ++i) {
         for (size_t j=0; j<nColumns; ++j) {
            size_t indexNew = convertToIndex(j, i, c, r*c);
            size_t indexOld = convertToIndex(j, i);
            growth[indexNew] = cont[indexOld];
         }
      }
      nRows = r;
      nColumns = c;
      maxSize = r * c;
      cont = growth;
   }

   double get(size_t row_i, size_t col_i) const {
      return cont[convertToIndex(col_i, row_i)];
   }

   void set(size_t row_i, size_t col_i, double val) {
      cont[convertToIndex(col_i, row_i)] = val;
   }

   void printHeader() const {
      for (size_t col = 0; col < nColumns; ++col) {
         size_t width = 5;
         if (col == 0) { width = 10; }
         cout << setw(width) << col + 1;
      }
      cout << endl;
   }

   void printRow(size_t r) const {
      cout << fixed << setprecision(1);
      for (size_t col = 0; col < nColumns; ++col) {
         double d = get(r, col);
         cout << setw(5) << d;
      }
   }

   void print() const {
      if (nRows == 0 || nColumns == 0) { return; }
      printHeader();
      for (size_t row = 0; row< nRows; ++row) {
          cout << setw(5) << row + 1;
          printRow(row);
          cout << endl;
      }
   }

private:
   size_t maxSize;
   size_t nRows;
   size_t nColumns;
   vector<double> cont;

   static inline size_t convertToIndex(
      size_t col_i, size_t row_i, size_t numCols, size_t maxSize) {
      size_t index = row_i* numCols + col_i;
      if (index >= maxSize) { 
         throw out_of_range("calculated index past the end of the vector");
      }
      return index;
   }

   inline size_t convertToIndex(size_t col_i, size_t row_i) const {
      return convertToIndex(col_i, row_i, nColumns, maxSize);
   }
};

int main() {
   try {
      Matrix m(3,3);
      for (size_t i=0; i<3; ++i) {
          m.set(i,i, 1.0);
      }

      Matrix b(m);
      b.grow(5, 7);
      b.print();
   }
   catch (const exception& exp) {
      cerr << "EXCEPTION: " << exp.what() << endl;
   }

   return 0;
}

【讨论】:

  • 好吧,使用您编写的第一个代码似乎也崩溃了。我已经调试过,问题似乎出在默认构造函数和我尝试使用的构造函数中。在第 13 行和第 17 行,它尝试访问此行: retval = HeapFree(_crtheap, 0, pBlock);在 free.c 调试器状态中:加载 'C:\Windows\SysWOW64\ntdll.dll',找不到或打开 PDB 文件 0xC0000005:访问冲突读取位置 0x00000004。我很想看看你会如何解决它的例子!
  • 添加了我的实现。
猜你喜欢
  • 2019-12-19
  • 1970-01-01
  • 2018-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-12
  • 1970-01-01
  • 2023-04-11
相关资源
最近更新 更多