Return vector from function without it being destroyed

19,691

Solution 1

You can create anything on heap with new. You shouldn't give out from the function the references to your stack objects, as they will be destroyed as soon as the function finishes.

If you prefer your function to return the vector by value, be sure that the objects inside the vector implement copy constructor (and perhaps assignment operator, too, not sure about that). Having that, please do not forget about the Rule of Three.

Solution 2

C++11 should solve your problem using rvalue references. Honestly, I haven't tried it myself, but from what I read it will do exactly what you are trying to do, return the vector without destroying and recreating it (by passing the memory allocated by that vector on to the new vector instead of having the new vector create its own memory and copy the contents over from the old one).

Share:
19,691
Alex
Author by

Alex

Updated on June 04, 2022

Comments

  • Alex
    Alex almost 2 years

    I have come across an interesting problem. I have a function in C++ that returns a vector filled with classes. Once the vector is returned, it calls deconstructors for each class that is element in the vector.

    The problem is an obvious one: the data is destroyed where a class points to the pointers, which get released when the object is destroyed. I can only assume the deconstructors are called because the vector is on the stack, and not on the heap.

    So the question is:

    Is there anyway to keep returning vector from a function, without it being destroyed? Or would I have to either pass a pointer to return vector as an input to the function?

    • sstn
      sstn over 13 years
      Are you returning a reference to a temporary object? It is not really clear to me what you are describing. Source code?
    • Alex
      Alex over 13 years
      @sstn: Well I return an entire vector, not a pointer to it.
    • sstn
      sstn over 13 years
      I assume you solved your problem - but it is only possible to speculate without seeing actual code. Btw, a reference is not a pointer.
  • Alex
    Alex over 13 years
    Can that be done using std::vector<int> result = new std::vector<int>();?
  • Vlad
    Vlad over 13 years
    @Alex: No, you need std::vector<int>* pResult = new std::vector<int>();. And you'd need to access your vector as pResult->push_back(...) instead of result.push_back(...).
  • Alex
    Alex over 13 years
    That's what I was wondering whether there was another way of doing it, not just pointer. Although I guess it should be pointer anyways because it will be better for memory consumption.
  • Vlad
    Vlad over 13 years
    @Alex: another way would be that the function gets the vector as argument (pointer or reference), and fills it with the content. This way could be actually better from the resource management point of view: you don't need to care about the deallocating of the vector on heap.
  • Mark Ransom
    Mark Ransom almost 12 years
    There's a possibility that the compiler will do return value optimization and avoid the copy as well, even without C++11. It's hard to recommend magic compiler optimizations unless you're sure they're really being used.