0

I need my vector container to persist past the end of the function that allocates it.

void initVector() {
    vector<obj1*> R;
}

Why is this not valid?

void initVector() {
    vector<obj1*> R = new vector<obj1*>;
}
Scholar
  • 293
  • 3
  • 8
  • 16
  • 5
    Could you not just return it? – chris Mar 13 '15 at 20:16
  • 3
    Why do you need to do that? And how do you intend to prevent a memory leak? – juanchopanza Mar 13 '15 at 20:17
  • 1
    Can't you just pass an empty vector as reference and do what you need to do in the function? – TriHard8 Mar 13 '15 at 20:17
  • 1
    Good question, but I agree with @juanchopanza. However, if you had to do it, it isn't valid because `new` returns a handle to the allocated memory. All you need to do to make it valid is to make `R` a pointer of type `std::vector`. Just like everyone is saying make sure you don't have a memory leak if you do call `new` make sure to call `delete` correctly. – Daniel Robertson Mar 13 '15 at 20:21

1 Answers1

7

It should be vector<obj1*>* R = new vector<obj1*>;. However, with C++11's move semantics, using pointers or references to avoid expensive copies is usually not needed. In the case of a vector, the below code doesn't copy tempVec - it will move it at least. More likely that compiler will elide the copy. You can read more about it here.

vector<obj1*> getVector()
{
 vector<obj1*> tempVec;
 //populate tempVec;
 return tempVec;//The standard guarantees a move at least. Compilers are free to elide this copy.
}

EDIT : As @juanchopanza points out in the comments, if you have a good reason to be doing this, be wary of managing the lifetime of the vector allocated on the heap, R. This isn't an issue specific to this statement - it applies to dynamically allocated structures in general. However, it might need special mention here since you have a vector of pointers. In addition to correctly managing the lifetime of R, you also need to carefully manage the lifetimes of the obj1* it contains. Objects allocated on the heap don't get deleted when the pointer holding them goes out of scope. You will have to manually call delete on a pointer to that memory location when you are done using it. This applies both to the vector* R and, unless owned by some other part of your program, to the obj* that it contains.

Community
  • 1
  • 1
Pradhan
  • 16,391
  • 3
  • 44
  • 59
  • 3
    And then they have a memory leak. – juanchopanza Mar 13 '15 at 20:17
  • 1
    That's a weird assumption. The issue OP had was with that line of code. That was the question and that is what this answers. Your comment is like saying "You will have a memory leak" to someone asking a question about the syntax of `new`. I am not suggesting that they `new` the `vector` and leave it there, without bothering to clean it or worry about the pointers it contains. – Pradhan Mar 13 '15 at 20:19
  • @Pradhan: The point of the question is to get a valid `std::vector` even after the end of the function. With your implementation, either he will not `delete` the `vector` properly, leaving a memory leak, or he will `delete` it properly and not have what he wants (a valid `vector`). Either way, your approach doesn't help him. – wolfPack88 Mar 13 '15 at 20:22
  • 2
    It's not a weird assumption given that OP clearly doesn't know basic things such as `new` returns a pointer. – juanchopanza Mar 13 '15 at 20:23
  • This seems to be what I'm looking for. Can you explain why this statement leaks memory? – Scholar Mar 13 '15 at 20:28
  • 1
    @R1v2 Edited with more details. – Pradhan Mar 13 '15 at 20:36