In the following case my object goes out of scope and I access an invalid pointer:
struct Animal
{
char* buffer;
Animal() { buffer = new char[100]; }
~Animal() { delete[]buffer; }
};
int main()
{
vector<Animal> list;
{
Animal dog;
list.push_back(dog);
}
list[0].buffer[50] = 7; // Buffer's been deleted, can't access it
}
I guess the best way to prevent this would be to construct the Animal object in place in the vector, but I don't know how to do that. I thought about doing:
list.push_back(Dog());
But this still creates a temporary, unless it's optimised away, and I'd rather not rely on that because in another place (another compiler) it may not do the same thing.
Edit: Thanks to Remy Lebeau I've learned you can construct a vector element directly in the vector, no temporaries, no copying, with the function:
template< class... Args >
void emplace_back( Args&&... args );
I don't know how the variadic template arguments work, but the description is:
Appends a new element to the end of the container. The element is constructed through std::allocator_traits::construct, which typically uses placement-new to construct the element in-place at the location provided by the container. The arguments args... are forwarded to the constructor as std::forward(args)....