首页 > 解决方案 > 在函数中分配内存并返回它

问题描述

int** transpose(int** matrix,int row, int column)
{
    int** new_mat = new int*[column];


    for(int i = 0; i < column; i++)
    {
        new_mat[i] = new int[row];
    }
    for(int i  = 0; i < row; i++ )
    {
        for(int j = 0; j < column; j ++)
        {
            new_mat[j][i] = matrix[i][j];
        }
    }

    return new_mat;
}

我已经编写了这个函数,但是感觉有些不对劲我无法决定是否应该删除 new_mat 以某种方式基本上函数返回这个值我应该如何在不使用任何智能指针或其他东西的情况下管理内存?

标签: c++memory-management

解决方案


如果您不想使用任何智能指针和向量,请尝试这样。 matrix - 是大小为 [col][row] 的 2D 动态数组的包装器。

#include <iostream>
#include <algorithm>

using namespace std;

class matrix
{
private:
    unsigned int m_row;
    unsigned int m_col;
    int **m_data;

public:    
    matrix(unsigned int row, unsigned int col) : m_row(row), m_col(col), m_data(nullptr)
    {
        alloc();
    }

    matrix(const matrix &m) : m_row(m.get_rows_count()), m_col(m.get_cols_count()), m_data(nullptr)
    {
        alloc();
        for(unsigned int i = 0; i < m_row; i++)
            for(unsigned int j = 0; j < m_col; j++)
                m_data[i][j] = m[i][j];
    }

    ~matrix()
    {
        free();
    }

    unsigned int get_rows_count() const { return m_row; }
    unsigned int get_cols_count() const { return m_col; }
    const int* operator[](unsigned int ind) const
    {
        return m_data[ind];
    }
    int* operator[](unsigned int ind)
    {
        return m_data[ind];
    }

    matrix& operator=(const matrix &m)
    {
        free();
        m_row = m.get_rows_count();
        m_col = m.get_cols_count();
        alloc();
        for(unsigned int i = 0; i < m_row; i++)
            for(unsigned int j = 0; j < m_col; j++)
                m_data[i][j] = m[i][j];
        return *this;
    }

// you need move-operations:
    //matrix(matrix&& other) = delete;       // move constructor (rule of 5)
    //matrix& operator=(matrix&& other);     // move assignment  (rule of 5)

    void print()
    {
        for(unsigned int i = 0; i < m_row; i++)
        {
            for(unsigned int j = 0; j < m_col; j++)
                cout << m_data[i][j] << " ";
            cout << endl;
        }
    }

private:
    void alloc()
    {
        if(m_data)
            return;
        m_data = new int*[m_row];
        for(unsigned int i = 0; i < m_row; i++)
        {
            m_data[i] = new int[m_col];
            std::fill(m_data[i], m_data[i] + m_col, 0);
        }
    }
    void free()
    {
        if(!m_data)
            return;
        for(unsigned int i = 0; i < m_row; i++)
            delete[]m_data[i];
        delete[]m_data;
        m_data = nullptr;
    }
};

matrix transpose(const matrix matrix_in)
{
    unsigned int M = matrix_in.get_rows_count();
    unsigned int N = matrix_in.get_cols_count();
    matrix out(N, M);

    for(unsigned int i = 0; i < M; i++)
        for(unsigned int j = 0; j < N; j++)
            out[j][i] = matrix_in[i][j];
    return out;
}


int main(int argc, char* argv[])
{
    matrix m1(5, 7);
    m1[0][1] = m1[0][2] = m1[0][3] = 7;
    auto m2 = transpose(m1);
    m1.print();
    cout << endl;
    m2.print();
}

无论如何,最好在分配内存的地方释放内存。如果你不想使用某些类,你可以这样做:

void transpose(int **matr_in, int **matr_out, int M, int N)
{
    for(int i = 0; i < M; i++)
        for(int j = 0; j < N; j++)
            matr_out[j][i] = matr_in[i][j];
}

int **create_matrix(int M, int N)
{
    int **m = new int*[M];
    for(int i = 0; i < M; i++)
        m[i] = new int[N];
    return m;
}

void delete_matrix(int **m, int M)
{
    for(int i = 0; i < M; i++)
        delete []m[i];
    delete []m;
}


int main()
{
    int M = 5, N = 4;
    int **m1 = create_matrix(M, N);
// fill matrix m1
    int **m2 = create_matrix(N, M);

    transpose(m1, m2, M, N);

    delete_matrix(m1, M);
    delete_matrix(m2, N);

    return 0;
}

推荐阅读