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 回答