Question

Let's say my function:

vector<MyClass>* My_func(int a)
{
    vector<MyClass>* ptr = new vector<MyClass>;
    //...... Add a lot of elements to this vector, and let's say MyClass is also relatively big structure.

    return ptr;
}

This method leaves responsibility for user to free the pointer.

Another method I can think of is just creating local variable in function and return the value:

vector<MyClass> My_func(int a)
    {
        vector<MyClass> vec;
        //...... Add a lot of elements to this vector, and let's say MyClass is also relatively big structure.

        return vec;
    }

This one avoid the responsibility for user but may take a lot of space when return and copy the value.

Maybe smart pointer in C++ is a better choice but I am not sure. I did not use smart pointer before. What do people do when they come across this situation? What kind of return type will they choose?

Thanks ahead for your tips:-)

Was it helpful?

Solution

In most cases of this sort of construct, the compiler will do "Return Value Optimisation", which means that it's not actually copying the data structure being returned, but instead writing straight into one that lives on the place where it will be returned to.

So, you can safely do this without worrying about it being copied.

However, another method would be to not return a vector in the first, place, but request that the calling code pass one in:

So, something like:

 void My_func(int a, vector<MyClass>& vec)
 {
   ... 
 }

This is GUARANTEED to avoid copying.

OTHER TIPS

In many situations return value optimizations easily can take care of the unnecessary copying. The question is: how are you planning to use this vector outside the function? If you have something like:

vector<MyClass> ret = My_func(a);

Then the optimizations can take care of the problem.

On the other hand, if you want to reuse an existing vector, you could pass a non-const reference to an existing vector, but there aren't many situations where this is needed or useful.

vector<MyClass> ret;
// do something with ret ...
My_func(a, ret);

Plus, this also changes the semantics of your function (e.g. you may need to clear() the vector).

Here is the internal structure of a vector

template <typename T>
class vector {
private:
  size_t m_size;
  size_t m_cap;
  T * m_data;
public:
//methods push pop etc.
};

As you can the size of the vector (with 2 additional size_t data members) is not much larger than size of a pointer. There will be negligible performance benefit in passing a vector instead of pointer, infact using a pointer, accessing the vector will be slower as each time you will have to dereference the pointer. Generally, we don't create a pointer to a vector.

Also, never return a pointer to a local variable. The memory of the local variable will be wiped off once you return value & go out of the scope of the method. Ideally, you should create a vector in the calling function and pass a reference to the vector, when calling your method My_func.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top