繁体   English   中英

_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse))错误

[英]_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)) error

我试图弄清楚为什么我的程序在运行时失败了。 到目前为止,当我运行我的程序时,它失败了。 我调试了错误,它将我带到了dbgdel.cpp。 第32行“_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse));”。 我搜索到的答案无济于事,但它与我的内存泄漏有关?

这是我的标题:

#include <iostream>
using namespace std;
namespace project
{
#ifndef MATRIX_H
#define MATRIX_H

typedef int* IntArrayPtr;
class Matrix
{
public:
    friend ostream& operator<<(ostream& out, Matrix& object);
    //friend istream& operator>>(istream& in, Matrix& theArray);
    //Default Constructor
    Matrix();
    Matrix(int max_number_rows, int max_number_cols, int intial_value);
    //Destructor
    ~Matrix();
    //Copy Constructor
    Matrix(const Matrix& right_side);
    //Assignment Operator
    Matrix& operator=(const Matrix& right_side);

    void Clear();
    int Rows();
    int Columns();
    bool GetCell(int x,int y, int& val);
    bool SetCell(int x,int y, int val);
    int getNumber(int r, int c);
    //void Debug(ostream& out);
private:
    int initialVal;
    int rows;
    int cols;
    IntArrayPtr *m;
};
#endif
}

我的实施文件:

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

namespace project
{
Matrix::Matrix()
{
typedef int* IntArrayPtr;
IntArrayPtr *m = new IntArrayPtr[1];
for(int i = 0; i < 1; i++)
    m[i] = new int[1];

for(int r = 0; r < 1; r++)
    for(int c = 0; c < 1;c++)
        m[r][c] = 0;
}
Matrix::Matrix(int max_number_rows, int max_number_cols, int initial_value)
{
initialVal = initial_value;
rows = max_number_rows;
cols = max_number_cols;
IntArrayPtr *m = new IntArrayPtr[rows];
for(int i = 0; i < rows; i++)
    m[i] = new int[cols];

for(int r = 0; r < max_number_rows; r++)
    for(int c = 0; c < max_number_cols;c++)
        m[r][c] = initial_value;

}
Matrix::~Matrix()
{
for(int i = 0; i <= rows; i++)
    delete [] m[i];
delete [] m;
}
void Matrix::Clear()
{
for(int r = 0; r < rows; r++)
    for(int c = 0; c < cols;c++)
        m[r][c] = initialVal;
}

int Matrix::Rows()
{
return rows;
}

int Matrix::Columns()
{
return cols;
}

bool Matrix::GetCell(int x,int y, int& val)
{
if(x < rows || y < cols)
    return false;
else
{
    val =   m[x - 1][y - 1];
    return true;
}
}

bool Matrix::SetCell(int x, int y, int val)
 {
if(x < rows || y < cols)
    return false;
else
{
    m[x - 1][y - 1] = val;
    return true;
}
}
int Matrix::getNumber(int r, int c)
{
return m[r][c];
}

ostream& operator<<(ostream& out, Matrix& object)
{

for(int r = 0; r < object.rows; r++)
{
    for(int c = 0; c < object.cols; c++)
    {
        out << " " << object.m[r][c];
    }
out << endl;
}
return out;
}

Matrix& Matrix::operator=(const Matrix& right_side)
{
if (this != &right_side)
{
    rows = right_side.rows;
    cols = right_side.cols;
    delete[] m;
    IntArrayPtr *m = new IntArrayPtr[rows];
    for(int i = 0; i < rows; i++)
        m[i] = new int[cols];

    for(int r = 0; r < rows; r++)
        for(int c = 0; c < cols;c++)
            m[r][c] = right_side.initialVal;
}
return *this;
}

/*
void Matrix::Debug(ostream& out)
{
out << "Number of rows = " << rows << endl;
out << "Number of columns = " << cols << endl;
out << "Initializer = " << initialVal << endl;
out << "Current contents of the matrix: " << endl;
out << m << endl;
}
*/
/*
istream& operator >>(istream& in, Matrix& theArray)
{
in >> 


}

void interfaceMatrix()
{
int userChoice, rows, columns, value;

cout << "Default matrix or custom(1 for default, 0 for custom): ";
cin >> userChoice;
    if (userChoice == 1)
    {
        Matrix object;
        return object;
    }
    else if(userChoice == 0)
    {
        cout << "Enter number of rows: ";
        cin >> rows;
        cout << "Enter number of columns: ";
        cin >> columns;
        cout << "Enter initial value of each element: ";
        cin >> value;
        if(rows <= 0 || columns <= 0)
        {
            cout << "Invalid input." << endl;
            exit(1);
        }
        else
        {
            Matrix object(rows, columns, value);
            return object;
        }
    }
    else
    {
        cout << "Invalid choice." << endl;
        exit(1);
    }

}
*/
}

在我的驱动程序中,我只使用了Matrix测试(2,2,2),因此我可以为每个元素创建一个初始值为2的2 x 2数组。 我得到了上述错误。

您正在分配rows数,但释放rows+1行数。 检查析构函数。 <=必须<。

除此之外,您的代码中还有很多其他[潜在]错误:

  • 您正在设置本地m变量而不是设置类的m数据成员(这就是为什么我的约定总是在数据成员之前用m_来防止这种混淆)。 此错误同时出现在构造函数和赋值运算符中。
  • 您使用rows来分配矩阵,但使用max_number_rows来初始化矩阵。 虽然它现在可以正常工作,它可能会导致错误,如果row在后面不同的初始化(例如,如果row与初始化std::max(max_number_rows,1000)你在赋值操作符码是更好的。
  • 你在GetCell和SetCell中的if-test是不正确的。 它应该是>=而不是<
  • 赋值运算符复制矩阵的大小,但为所有单元格分配initialValue。 这不是一项任务。 此实现可能/将混淆其余代码。
  • IntArrayPtr的typedef是不必要的

两个问题:

  1. 您没有将任何值分配给对象的“m”成员,而是分配给名为“m”的局部变量
  2. 你通过从i = 0循环到i <=行来解除分配,你想要i = 0到i <行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM