C++ 释放内存时出错

C++ 释放内存时出错,c++,memory-management,free,C++,Memory Management,Free,以下是文件: //============================================================================ // Name : linearMatrix.hpp // Author : Flores, Facundo Gabriel // Version : 0.1 // Description : This file contains the linearMatrix class. //======

以下是文件:

//============================================================================
// Name        : linearMatrix.hpp
// Author      : Flores, Facundo Gabriel
// Version     : 0.1
// Description : This file contains the linearMatrix class.
//===========================================================================

#ifndef LINEARMATRIX_HPP_
#define LINEARMATRIX_HPP_

class linearMatrix
{
public:
    //Constructor
    linearMatrix(const int Width, const int Heigth);

    //Destructor
    ~linearMatrix();

    //We read a matrix file
    void Read_File_Matrix(const char *Path);

    //We write a matrix file
    void Write_File_Matrix(const char *Path);

    //Generate a diagonal dominant matrix
    void Generate_Diagonal_Dominant(void);

    //Generate a random matrix
    void Generate_Random_Matrix(void);

    //Copy a float *vector
    void Copy_Vector(const float *V, const int Width, const int Heigth);

    //Show a little vector
    void Show_Little_Matrix(void);

private:
    int _Width, _Height; // Width and Height
    float* myVector;

    //Aux function for testing
    bool Test_Sizes(const int Width, const int Heigth);
};



#endif /* LINEARMATRIX_HPP_ */


//============================================================================
// Name        : linearMatrix.cpp
// Author      : Flores, Facundo Gabriel
// Version     : 0.1
// Description : This file contains the linearMatrix implementation class.
// A linear matrix is a vector that represents a matrix.
// We will read this kind of classes from files, because
// they are generally large. But linearMatrix can represent
// a 1D vector if Height is 1.
//============================================================================



#include <iostream>
using std::cout;
using std::endl;
using std::cerr;
using std::bad_alloc;

#include <cstdio>

#include <cstdlib>

#include <ctime>

#include <new>

#include "linearMatrix.hpp"


#define FALSE 0
#define TRUE 1

//Test if W and H are correct
bool linearMatrix::Test_Sizes(const int W, const int H)
{
    if(W < 1 || W > 32500)
        return FALSE;
    if(H < 1 || H> 32500)
        return FALSE;
    return TRUE;
}

// We set the Width and Height parameters
linearMatrix::linearMatrix(const int Width, const int Height)
{
    if(Test_Sizes(Width, Height))
    {
        _Width = Width;
        _Height = Height;
        try{
            myVector = new float[_Width * _Height];
        }
        catch(bad_alloc &ex)
        {
            cerr << "Exception:" << ex.what();
            exit(1);
        }
    }
    else
    {
        cout<<"Bad sizes!"<<endl;
        exit(1);
    }

}

linearMatrix::~linearMatrix()
{
    delete [] myVector;
}

//We set a random vector using its Width and Height
void linearMatrix::Generate_Random_Matrix(void)
{
    srand(time(NULL));
    for(int i = 0; i < _Width; i++)
        for(int j = 0; j < _Height; j++)
        {
            // We are putting a number between 0 and 99
            myVector[i * _Width + _Height] = rand() % 100;
        }
}

//We set a diagonal dominant matrix
void linearMatrix::Generate_Diagonal_Dominant(void)
{
    for(int i = 0; i < _Width; i++)
        for(int j = 0; j < _Height; j++)
        {
            if(i == j) //Diagonal item
                myVector[i * _Width + j] = 32500;
            else
                myVector[i * _Width + j] = 0.5;
        }
}

//We copy V into myVector
void linearMatrix::Copy_Vector(const float *V, const int Width, const int Heigth)
{
    if(Width != _Width || Heigth != _Height)
    {
        cout<<"Different sizes, we cannot copy vector V"<<endl;
        exit(1);
    }
    else
    {
        for(int i = 0; i < _Width; i++)
            for(int j = 0; j < _Height; j++)
            {
                myVector[i * _Width + j] = V[i * Width + j];
            }
    }
}

//We show a little matrix. Assume H < 11 and W < 11
void linearMatrix::Show_Little_Matrix(void)
{
    cout.precision(5);
    if(_Height < 11 && _Width < 11)
    {
        for(int i = 0; i < _Width; i++)
        {
            for(int j = 0; j < _Height; j++)
            {
                cout<<myVector[i * _Width + j]<<" ";
            }
            cout << endl;
        }
    }
    else
    {
        cout << "I can show only little matrices in the console " << endl;
    }
}

void linearMatrix::Write_File_Matrix(const char *Path)
{
    FILE *pFile = fopen(Path, "wb");
    fwrite(&_Height, sizeof(int), 1, pFile);
    fwrite(&_Width, sizeof(int), 1, pFile);
    fwrite(myVector, sizeof(float), _Height * _Width, pFile);
    fclose(pFile);
}

void linearMatrix::Read_File_Matrix(const char *Path)
{
    FILE *pFile = fopen(Path, "rb");
    if(pFile == NULL){
        cout << "Cannot read :" << Path << endl;
        exit(1);
    }
    fread(&_Height, sizeof(int), 1, pFile);
    fread(&_Width, sizeof(int), 1, pFile);

    try{
        myVector = new float[_Width * _Height];
    }
    catch(bad_alloc &ex)
    {
        cout << "Exception:" << ex.what();
        exit(1);
    }
    fread(myVector, sizeof(double), _Height * _Width, pFile);
}
因此,错误是:

我还不知道如何使用valgrind。但为什么会出现错误呢?程序向我显示正确的输出,但当析构函数“执行”时,释放
myVector
时会出现错误。如何解决它?

这里有一个错误

void linearMatrix::Generate_Diagonal_Dominant(void)
{
    for(int i = 0; i < _Width; i++)
        for(int j = 0; j < _Height; j++)
        {
            if(i == j) //Diagonal item
                myVector[i * _Width + j] = 32500;
            else
                myVector[i * _Width + j] = 0.5;
        }
}
void linearMatrix::生成对角线(void)
{
对于(int i=0;i<\u Width;i++)
对于(int j=0;j<_Height;j++)
{
if(i==j)//对角项
myVector[i*_宽度+j]=32500;
其他的
myVector[i*_宽度+j]=0.5;
}
}
应该是

void linearMatrix::Generate_Diagonal_Dominant(void)
{
    for(int i = 0; i < _Width; i++)
        for(int j = 0; j < _Height; j++)
        {
            if(i == j) //Diagonal item
                myVector[j * _Width + i] = 32500;
            else
                myVector[j * _Width + i] = 0.5;
        }
}
void linearMatrix::生成对角线(void)
{
对于(int i=0;i<\u Width;i++)
对于(int j=0;j<_Height;j++)
{
if(i==j)//对角项
myVector[j*_宽度+i]=32500;
其他的
myVector[j*_宽度+i]=0.5;
}
}
换句话说,在计算偏移量时,i和j的方向是错误的(或者您应该使用
i*\u Height+j
)。你似乎自始至终都犯了同样的错误


您还有其他错误,如chris提到的不遵循三的规则。

以下划线开头,后跟大写字母的标识符,如您的
\u Width
,在任何范围内。我会记住这一点。你也没有注意到(五),这在当今时代应该是最重要的。你应该尽快学会如何使用valgrind和debugger。使用valgrind时,基本命令是:“valgrind--tool=memcheck--leak check=yes--show reachable=yes--num callers=20--track fds=yes./test”记住编译器中的调试器标志,在g++中是“-g”。@chris。以下划线开头的标识符是linearMatrix类的成员。我会读到关于三法则和零法则的书,我从来没有听过,谢谢。@Mycotoxin谢谢,我会尽我所能学习valgrind@facunvd,但“任意范围”的意思是宏可以命名为
\u Width
。如果在将来的实现更改中发生这种情况,您的类内
\u Width
仍然是错误的。相比之下,以一个下划线开头的标识符保留在全局范围中。这意味着实现中的宏不能(不应该)有一个名称,比如说,
\u foo
,因为它影响的不仅仅是全局范围,它没有保留一个名称供它使用。
void linearMatrix::Generate_Diagonal_Dominant(void)
{
    for(int i = 0; i < _Width; i++)
        for(int j = 0; j < _Height; j++)
        {
            if(i == j) //Diagonal item
                myVector[i * _Width + j] = 32500;
            else
                myVector[i * _Width + j] = 0.5;
        }
}
void linearMatrix::Generate_Diagonal_Dominant(void)
{
    for(int i = 0; i < _Width; i++)
        for(int j = 0; j < _Height; j++)
        {
            if(i == j) //Diagonal item
                myVector[j * _Width + i] = 32500;
            else
                myVector[j * _Width + i] = 0.5;
        }
}