Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ vector push_back

Tags:

c++

vector

What is the proper way of pushing a new object element onto a std::vector? I want the data to be allocated in the vector. Will this copy the object newradio into the vector and then get rid of newradio when it goes out of scope (e.g. out of the stack)?

vector<Radio> m_radios;
Radio newradio(radioNum);
m_radios.push_back(newradio);

And then when I free the object containing m_radios, will this free all the memory allocated by the vector?

like image 726
user623879 Avatar asked Sep 11 '11 23:09

user623879


People also ask

What does vector Push_back do?

C++ Vector Library - push_back() Function The C++ function std::vector::push_back() inserts new element at the end of vector and increases size of vector by one.

What does Push_back mean?

The push_back() function is used to insert an element at the end of a vector. This function is available in the <vector> header file.

What does Push_back return in C++?

It does not have any return value.

Is vector Push_back expensive?

That's a depends. Normally push_back is pretty cheap, but every now and then it has to resize to fit more elements, and that can be costly. In this case you can gain a lot by passing references or taking advantage of move semantics so that you aren't copying Object s around when you could be moving them.


3 Answers

std::vector manages its own memory. That means that, when the destructor of a vector is invoked the memory held by the vector is released. std::vector also invokes an object's destructor when it is removed (through erase, pop_back, clear or the vector's destructor).

When you do this:

Radio newradio(radioNum); m_radios.push_back(newradio); 

You add a copy of newradio (created using Radio's copy constructor) to the vector. newradio will be destroyed when it goes out of scope, and the copy will be destroyed when it is removed from the vector (as for any object).

That's an important point: std::vector only stores copies of an object, which means the object must have a meaningful copy constructor (and an assignment operator, but that's another issue). If you have a vector of pointers, then the pointer itself will be copied, and not what it points to. Note that this behavior is the same for every standard container (like std::list or std::set).

As a rule of thumb, if you're not using pointers, then you don't have to worry about releasing the memory yourself.

In C++11, most standard containers (including vector) have an emplace_back method that constructs an object in place at the end of the container. It takes a bunch of parameters and calls the constructor that best matches those parameters (or fails if no such constructor exist), using said constructor to create the object, without any copy, at the end of the container. So, the above code could be rewritten as:

m_radios.emplace_back(radioNum); // construct a Radio in place,                                   // passing radioNum as the constructor argument 

Also in C++11, containers are usually move aware, so they no longer require objects to be copiable: if they are movable, then the container will move its contents as required (such as during reallocations, for instance). Copiable types are still required if you want to copy the vector.

like image 122
Etienne de Martel Avatar answered Sep 21 '22 14:09

Etienne de Martel


push_back() will store a copy of its argument within the vector. As long as Radio implements proper value semantics, there will be no problems with it.

like image 23
K-ballo Avatar answered Sep 21 '22 14:09

K-ballo


Yes pushing newRadio will push a copy of the Radio object into the vector. You also don't need to free the vector because it is local only so it will be destroyed once you are out of its scope. If for instance you wrote

vector<Radio> *m_radios = new vector<Radio>();

Then you would have to free that memory by calling the vector destructor manually.

like image 26
Jesus Ramos Avatar answered Sep 21 '22 14:09

Jesus Ramos