簡體   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