2012-09-08 65 views
1

的就是這些文件:錯誤而釋放內存

//============================================================================ 
// 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); 
} 

而且我想使用這個類有以下main.cpp

//============================================================================ 
// Name  : main.cpp 
// Author  : Flores, Facundo Gabriel 
// Version  : 0.1 
// Description : This file is a test. 
//=========================================================================== 
#include <iostream> 
#include "linearMatrix.hpp" 

int main(void) 
{ 
    linearMatrix Matrix2(3,1); 

    Matrix2.Generate_Diagonal_Dominant(); 
    Matrix2.Show_Little_Matrix(); 

    return 0; 
} 

因此錯誤是:

*** glibc detected *** /.../linearMatrix/Debug/linearMatrix: free(): invalid next size (fast): 0x00000000007f1010 *** 

我不知道如何使用valgrind呢。但爲什麼錯誤在那裏?該程序向我顯示正確的輸出,但是當析構函數「執行」時,錯誤出現,同時釋放了myVector。如何解決它?

+1

標識符以下劃線開頭,後跟一個大寫字母,比如'_Width',是[保留](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using -an-underscore-in-ac-identifier)在任何範圍內。我會牢記這一點。你也沒有注意[三規則](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-ree)(五),這應該是[規則(http://rmartinho.github.com/2012/08/15/rule-of-zero.html)在這個時代。 – chris

+1

你應該學習如何儘可能快地使用valgrind和調試器。使用valgrind基本命令是:「valgrind --tool = memcheck --leak-check = yes --show-reachable = yes --num-callers = 20 --track-fds = yes ./test」記住關於調試器標誌的內容你的編譯器,在g ++中它是「-g」。 – CyberGuy

+0

@chris。以下劃線開頭的標識符是類linearMatrix的成員。我將閱讀關於三規則和零規則的內容,我從來沒有聽過他們,謝謝。 – FacundoGFlores

回答

2

這裏有一個錯誤

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; 
     } 
} 

換句話說,你有i和j以錯誤的方式,當你計算偏移(或者也許你應該使用i * _Height + j)。你似乎在整個過程中犯了同樣的錯誤。

您還有其他錯誤,如未遵循規則的三個,如克里斯提到的。