C ++中向量的动态内存分配 [英] Dynamic Memory Allocation of Vectors in C++

查看:153
本文介绍了C ++中向量的动态内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写一个函数来添加两个3D数组的元素,将结果存储在第三个3D数组中,获取结果并将其复制到一维向量中,然后通过常量引用返回此向量。我遇到的问题是如何使用动态内存分配返回向量,因为你不能返回一个局部变量。这是代码看起来像,如果你可以返回一个局部变量:

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

//添加数组x和y
的值for(int i = 0; i {
for(int j = 0; j {
for(int k = 0; k {
z [i] [j] = x [i] [j] [k] + y [i] [j] [k]
}
}
}

int l = 0;
//将数组z的值放入向量
for(int i = 0; i {
for(int j = 0; j < width; j ++)
{
for(int k = 0; k {
results [ [k];
l ++;
}
}
}
返回结果;
}

这是我使用动态内存分配的不正确尝试:

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

//添加数组x和y
的值for(int i = 0; i {
for(int j = 0; j {
for(int k = 0; k {
z [i] [j] = x [i] [j] [k] + y [i] [j] [k]
}
}
}

int l = 0;
//将数组z的值放入向量
for(int i = 0; i {
for(int j = 0; j < width; j ++)
{
for(int k = 0; k< depth; k ++)
{
* results [1] = z [i] [j ] [k];
l ++;
}
}
}
return * results;
}


解决方案

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



有多种方法可以解决这个问题。






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

  //返回向量的副本。 
vector< T> allOperations(T *** const& x,T *** const& y,T ***& z,int height,int width,int depth)
{
vector&结果(height * width * depth);
// ...
返回结果;
}



如果编译器执行 copy elision 和/或支持 C ++ 11移动构造函数,复制永远不会发生,返回将非常有效。






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

  //返回一个指向新分配的向量的指针。 
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>
// ...
返回结果;
}



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






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

  template<类型名T> 
class SomeClass
{
std :: vector< T>结果;
public:
// ...

//修改成员并返回对内部成员的引用。
const vector< T>& allOperations(T *** const& x,T *** const& y,T ***& z,int height,int width,int depth)
{
results.resize高度宽度深度);
// ...
返回结果;
}
};






另一种可能, / em>解决方案是返回对一些全局变量的引用。

  std :: vector< T&结果; 

//修改对全局变量的全局和返回引用。
const vector< T>& allOperations(T *** const& x,T *** const& y,T ***& z,int height,int width,int depth)
{
results.resize高度宽度深度);
// ...
返回结果;
}

或者,它的伪装(但完全相同)的形式:

  //修改全局变量并返回对全局变量的引用。 
const vector< T>& all操作(T *** const& x,T *** const& y,T ***& z,int height,int width,int depth)
{
//全局变量其名称在函数外部不可见。
static std :: vector< T>结果;

results.resize(height * width * depth);
// ...
返回结果;
}


I need to write a function that adds the elements of two 3D arrays, stores the result in a third 3D array, takes the results and copies them into a one dimensional vector, then returns this vector by constant reference. The problem I am having is how to use dynamic memory allocation to return the vector since you cannot return a local variable. This is what the code looks like if you COULD return a local variable:

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;
}

Here is my incorrect attempt at using dynamic memory allocation:

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;
}

解决方案

It does not make sense to return a reference to a local variable. If you resort to your second solution (return a reference to a new'd vector), then you will almost certainly have a memory leak. By convention, obtaining a reference usually means some other entity is already managing the returned object's lifetime.

There are multiple things you can do to work around this problem.


Don't perform dynamic memory allocation, change the function signature.

// 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;
}

If you compiler performs copy elision and/or supports C++11 move constructors, the "copy" will never take place and the return will be very efficient.


If you really want to dynamically allocate the vector to satisfy some other constraint, you also need to change the function signature:

// 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;
}

If you do so, consider returning a smart pointer instead of returning a bare pointer.


If this is a member function, then perhaps you can store the vector inside the object.

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;
    }
};


Another possible, but strongly discouraged solution is to return a reference to some global variable.

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;
}

Or, in its disguised (but exactly equivalent) form:

// 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 ++中向量的动态内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆