2012-09-11 51 views
1

這是我的代碼:我是否正確使用delete []運算符?

linearMatrix lAmatrixmatrixmult(const linearMatrix &A, 
          const linearMatrix &B) 
{ 
    //Local variables 
    int W_A = A.Get_Width; 
    int H_A = A.Get_Height; 

    int H_B = B.Get_Height; 
    int W_B = B.Get_Width; 

    float *vectorA; 
    float *vectorB; 
    float *vectorC; 
    //================ 

    //Memory allocation 
    try{ 
     vectorA = new float[W_A * H_A]; 
    } 
    catch(bad_alloc &ex) 
    { 
     cerr << "Exception:" << ex.what(); 
     exit(1); 
    } 

    try{ 
     vectorB = new float[W_B * H_B]; 
    } 
    catch(bad_alloc &ex) 
    { 
     cerr << "Exception:" << ex.what(); 
     exit(1); 
    } 

    try{ 
     vectorC = new float[W_A * H_B]; 
    } 
    catch(bad_alloc &ex) 
    { 
     cerr << "Exception:" << ex.what(); 
     exit(1); 
    } 
    //================= 

    //Initialization 
    vectorA = A.Get_Vector; 
    vectorB = B.Get_Vector; 
    //============== 

    if(W_A == H_B) 
    { 
     linearMatrix C(W_A, H_B); 

     for(int i = 0; i < W_A; i++) 
     { 
      for(int j = 0; j < W_B; j++) 
      { 
       float sum = 0; 
       for(int k = 0; k < W_A; k++) 
       { 
        float a = vectorA[i * W_A + k]; 
        float b = vectorB[k * H_B + j]; 
        sum += a * b; 
       } 
      vectorC[i * W_A + j] = sum; 
      } 
     } 
     C.Set_Vector(vectorC, W_A, H_B); 

     //Free memory 
     delete [] vectorA; 
     delete [] vectorB; 
     delete [] vectorC; 
     //=========== 

     return C; 
    } 
    else 
    { 
     cout << "Different sizes! Cannot perform mmmult" << endl; 

     //Free memory 
     delete [] vectorA; 
     delete [] vectorB; 
     delete [] vectorC; 
     //=========== 

     exit(1); 
    } 
} 

而且~linearMatrix是:

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

其中linearMatrix是:

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

    //Copy constructor 
    linearMatrix(const linearMatrix &that); 

    //Copy assignment operator 
    linearMatrix& operator=(const linearMatrix& that); 

    //Destroyer 
    ~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); 

    //Set a float *vector 
    void Set_Vector(const float *V, const int Width, const int Heigth); 

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

    //Get the vector 
    //Suppose V is previously allocated 
    float *Get_Vector(void); 

    //Get total number of elements 
    int Get_NumberofElements(void); 

    //Get Width 
    int Get_Width(void); 

    //Get Height 
    int Get_Height(void); 

private: 
    int myWidth, myHeight; // Width and Height 
    float* myVector; 

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

必備我留下一個函數,如果我用new前的空閒內存函數內部的運算符?我應該在任何exit()呼叫之前釋放記憶嗎?

+4

爲什麼你在使用'delete []'的時候,可以將值存儲在'std :: vector '中?快速,簡單,難以搞砸,如果您犯了錯誤,則更有可能獲得明智的錯誤。什麼是不喜歡? – Rook

+0

@andand對不起,我把所有相關的代碼都考慮進去了,因爲有時候人們會問我所使用的對象,在這種情況下,我需要知道釋放內存,我認爲驅逐艦可能是一個很好的問題。 – FacundoGFlores

+0

@好的,我會讀一下線性容器。謝謝! – FacundoGFlores

回答

6

不,那是不正確的:

vectorA = new float[W_A * H_A]; // allocates memory 
vectorA = A.Get_Vector();  // allocated memory is leaked 
           // vectorA now points to memory owned by A 
delete [] vectorA;    // delete memory owned by A 

當然,在C++中,你通常只使用std::vector<float>

+0

由於在問題中引用的代碼實際上說'vectorA = A.Get_Vector',它很可能甚至不會編譯,因爲Get_Vector是一個成員函數。我同意'... Get_Vector()'可能是想要的,但是,隨着問題的答案指出了這個意圖... – twalberg

+0

@亨裏克我認爲你是對的。那麼,我必須用vectorA指向從Get_Vector函數分配的內存?如果我使用std :: vector 我不擔心內存分配和釋放內存的權利? – FacundoGFlores

+0

@facunvd是的,使用std :: vector來代替。 –