1

これは私のコードです:

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

1 に答える 1

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>

于 2012-09-11T13:36:33.163 に答える