6

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?

3
  • 1
    Are you returning a reference to a temporary object? It is not really clear to me what you are describing. Source code?
    – sstn
    Feb 13, 2011 at 15:02
  • @sstn: Well I return an entire vector, not a pointer to it.
    – Alex
    Feb 13, 2011 at 15:11
  • I assume you solved your problem - but it is only possible to speculate without seeing actual code. Btw, a reference is not a pointer.
    – sstn
    Feb 13, 2011 at 15:20

3 Answers 3

4

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.

4
  • Can that be done using std::vector<int> result = new std::vector<int>();?
    – Alex
    Feb 13, 2011 at 15:04
  • 3
    @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(...).
    – Vlad
    Feb 13, 2011 at 15:05
  • 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.
    – Alex
    Feb 13, 2011 at 15:12
  • 2
    @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.
    – Vlad
    Feb 13, 2011 at 15:35
1

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).

1
  • 2
    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. Jun 1, 2012 at 3:08
0

C++ vectors are allocated on the heap. When you return a vector by value a new one will be created with copies of all the elements, then the original elements will be destroyed.

It sounds like you haven't defined your copy constructors properly. For example:

class ThisIsWrong
{
public:
    ThisIsWrong()
    {
        i = new int;
        *i = rand();
    }
    ~ThisIsWrong()
    {
        delete i;
        i = nullptr;
    }

    int value() const
    {
        return *i;
    }

private:
    int* i;
};

void foo()
{
    vector<ThisIsWrong> wronglets;
    wronglets.push_back(ThisIsWrong());
    return wronglets;
}

void main()
{
    vector<ThisIsWrong> w = foo();
    w[0].value(); // SEGFAULT!!
}

You either need to delete the copy and assignment constructors (which will then turn this into a compilation error instead of runtime), or implement them properly.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.