1

I am using vector as a buffer to store some objects to be later appended to another vector.

std::vector<Link*> buffer_vector;
std::vector<Link*> main_vector;
main_vector.insert(main_vector.end(),buffer_vector.begin(),buffer_vector.end());

The process of filling the buffer and appending occurs repeatedly. Therefore I need to clear the buffer at every iteration. My concern is if I use .erase or .clear methods to clear the buffer, the objects in the main vector will be deleted. Is this assumption correct? If yes, is there a workaround for that?

Thank you

vahid

1
  • 1
    To format code, select it and press Ctrl-K or else the {} button in the editor. Commented Mar 20, 2012 at 4:14

6 Answers 6

2

Your concern is incorrect.

main_vector.insert(main_vector.end(),buffer_vector.begin(),buffer_vector.end()); copies the elements from buffer_vector into main_vector.

These copied elements are totally separate from the original elements, and so they are not affected by modifications to the elements that they were copied from.

Sign up to request clarification or add additional context in comments.

Comments

1

If you mean clearing the buffer_vector will clear the contents in main_vector, no they are separate copies.

2 Comments

this is a vector of pointers. I am updating my question. please read after a few moments
the pointers get copied still - deleting the old pointers does nothing, the copied pointers point to the same object.
1

Insertion into a vector is done by copy. In other words, you're pushing a copy of the element into the new vector. A different copy will be deleted when the buffer vector is cleared.

(This assumes that you're pushing the same type into both vectors, and not something like a pointer to an element to a vector in the first vector into the second vector.)

3 Comments

oh yes! this is a vector of pointers. I am updating my question. please read after a few moments
Regardless of whether the elements are pointers or not, vector::insert will make copies of them, and so the operation is safe.
A copy of the pointer will be made then. Just make sure you delete the pointer once and only once. The vector will not delete for you, for as long as you only delete pointers out of your last vector, you should be fine (assuming every pointer makes its way there). You will, however, have issues if the pointer points to a stack-allocated element and it goes out of scope.
1

You are copying the elements into the vector, and they will stay there until you delete them.

1 Comment

Pointers or not , they are copied.
1

In the code you show, you are storing pointers into objects. The pointers will be copied, and nothing will happen to the allocated memory when the original vector is cleared or the elements erased.

Actually nothing will happen to the pointed memory when the second vector goes out of scope!! Your program leaks memory, you need to manually manage the pointers or else choose an appropriate smart pointer that you can use inside a vector (consider std::unique_ptr or std::shared_ptr from C++11 or the equivalents from boost or TR1).

2 Comments

The program only leaks memory if those pointers are pointing to objects which never get destroyed.
@Mankarse: Fair point, but since the question explicitly asks whether the objects in the main vector will be deleted my assumption is that he expects them to be deleted when the vector goes out of scope. If he is expecting it, I assume that the objects are allocated from the free store and never explicitly deleted
1

You may be confused about how a vector manages its objects. When you push_back into a vector, you make a copy. When you push_back a pointer, it copies the pointer. When you call clear() or erase() it deletes the pointer but not the object the pointer points to.

I am assuming you new'ed all your Link objects and added them to the vector container. You will need to delete them yourself, the container will not destroy the pointed to object for you (even if the container itself is destroyed when it goes out of scope). If you clear() both your containers before calling delete on all your Link objects, you will cause a memory leak (assuming you are not keeping pointers to Link objects somewhere else).

2 Comments

when i insert the first vector to another one and then .clear() the first vector, I have a track of those objects (i will delete them later) any other sign of memory link might be there?.
@rahman: If you have pointers to all the objects separate from the pointers in the arrays, then no, there should be no memory leak when you delete them through the original pointers (that are not in the containers). Although keep in mind that as soon as you delete the object(s) through any of the pointer aliases, all pointers pointing to that object are now invalid.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.