c++ - C++ 中 vector 的动态内存分配

标签 c++

我需要编写一个函数,将两个 3D 数组的元素相加,将结果存储在第三个 3D 数组中,获取结果并将它们复制到一维 vector 中,然后通过常量引用返回该 vector 。我遇到的问题是如何使用动态内存分配来返回 vector ,因为您不能返回局部变量。如果您可以返回局部变量,代码如下所示:

template <class T>
const vector<T>& allOperations(T*** const &x, T*** const &y, T*** &z, int height, int width, int depth)
{
    vector<T> results(height*width*depth);

    // Add the values of array x and y
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            for (int k = 0; k < depth; k++)
            {
                z[i][j][k] = x[i][j][k] + y[i][j][k];
            }
        }
    }

    int l = 0;
    //Places the values of array z into a vector 
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            for (int k = 0; k < depth; k++)
            {
                results[l] = z[i][j][k];
                l++;
            }
        }
    }
    return results;
}

这是我使用动态内存分配的错误尝试:

template <class T>
const vector<T>& allOperations(T*** const &x, T*** const &y, T*** &z, int height, int width, int depth)
{
    vector<T>* results(height*width*depth) = new vector<T>;

    // Add the values of array x and y
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            for (int k = 0; k < depth; k++)
            {
                z[i][j][k] = x[i][j][k] + y[i][j][k];
            }
        }
    }

    int l = 0;
    //Places the values of array z into a vector 
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            for (int k = 0; k < depth; k++)
            {
                *results[l] = z[i][j][k];
                l++;
            }
        }
    }
    return *results;
}

最佳答案

返回对局部变量的引用没有意义。如果您采用第二种解决方案(返回对 new vector 的引用),那么您几乎肯定会发生内存泄漏。按照惯例,获取引用通常意味着其他一些实体已经在管理返回对象的生命周期。

您可以采取多种措施来解决此问题。


不执行动态内存分配,更改函数签名。

// Return a copy of the vector.
vector<T> allOperations(T*** const &x, T*** const &y, T*** &z, int height, int width,  int depth)
{
    vector<T> results(height*width*depth);
    // ...
    return results;
}

如果编译器执行 copy elision和/或支持 C++11 move constructors ,“复制”将永远不会发生,返回将非常高效。


如果你真的想动态分配 vector 以满足其他一些约束,你还需要更改函数签名:

// Return a pointer to a newly allocated vector.
const vector<T>* allOperations(T*** const &x, T*** const &y, T*** &z, int height, int width, int depth)
{
    vector<T>* results(height*width*depth) = new vector<T>;
    // ...
    return results;
}

如果这样做,请考虑返回 smart pointer而不是返回一个裸指针。


如果这是一个成员函数,那么也许您可以将 vector 存储在对象中。

template<typename T>
class SomeClass
{
    std::vector<T> results;
public:
    // ...

    // Modify member and return reference to internal member.
    const vector<T>& allOperations(T*** const &x, T*** const &y, T*** &z, int height, int width,  int depth)
    {
        results.resize(height*width*depth);
        // ...
        return results;
    }
};

另一种可能但强烈反对的解决方案是返回对某个全局变量的引用。

std::vector<T> results;

// Modify global and return reference to global variable.
const vector<T>& allOperations(T*** const &x, T*** const &y, T*** &z, int height, int width,  int depth)
{
    results.resize(height*width*depth);
    // ...
    return results;
}

或者,以其伪装的(但完全等同的)形式:

// Modify global and return reference to global variable.
const vector<T>& allOperations(T*** const &x, T*** const &y, T*** &z, int height, int width,  int depth)
{
    // Global variable with name not visible outside the function.
    static std::vector<T> results;

    results.resize(height*width*depth);
    // ...
    return results;
}

关于c++ - C++ 中 vector 的动态内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9252523/

相关文章:

c# - P/从 wlanapi.dll 调用 WlanHostedNetworkQueryStatus

C++ 获取自某个日期以来的毫秒数

c++ - 使用 lambda 进行变体访问的最佳方法

c# - 在混合模式下调试(C++、C#、VB)

c++ - 在使用 Visual Studio 2005 调试时调用函数?

c++ - 读取二进制文件并转换为二进制字符串

c++ - `enable_if` 与 `enum` 模板特化问题

c++ - 为什么 mongo C++ 驱动程序给我编译错误?

c++ - 有没有办法从 python 编译 c/c++ 程序?

c++ - 为什么我用 Boost.ASIO 实现的简单 HTTP 服务器需要 sleep 才能正常工作