简体   繁体   中英

When using std::vector::push_back to insert an unknown number of elements, should std::vector::max_size be checked on each push?

I'm using std::vector in a context where some unknown number of elements are being inserted one at a time. std::length_error is thrown when std::vector::reserve would exceed the maximum size, but there appears to be no such guarantee for std::vector::push_back . My first instinct is to check whether my_vector.size() == my_vector.max_size() on each push, and throw an exception if the maximum would be exceeded.

If I do not make such a check, is it possible that the container would silently enter an invalid state? For example, could the size overflow?

Thank you!

No. push_back() will throw if it can't allocate memory.

max_size() is a theoretical limit. I've never seen it used. Maybe in a context with extremely limited memory and no exception support it could be useful, but in general no.

std::vector does not itself throw the exception on push_back , rather Allocate::allocator will throw the exception when you go over the max limit.

From https://en.cppreference.com/w/cpp/container/vector/push_back :

If an exception is thrown ( which can be due to Allocator::allocate() or element copy/move constructor/assignment), this function has no effect (strong exception guarantee).

...

Some implementations also throw std::length_error when push_back causes a reallocation that would exceed max_size, due to implicitly calling an equivalent of reserve(size()+1).

std::vector::max_size return a constant number. Typically, it is a 8 byte size integer which the vector size will never reach that limit if you run your code in a normal platform like a PC(because the value of 8 byte size integer is very huge, it is far far bigger than your machine's physical memory size + hard disk size), so my_vector.size() == my_vector.max_size() will never be true. Generally, you don't need worry about STL container report it can not allocate enough memory when the code run in normal computer.

Only in some embedded platform(has very limited memory), the std::vector::max_size might be small. Even in this kind of platform, the value of max_size still far bigger than the physical memory you can use. You need careful design your code which don't let that thing happen because if that thing happen, you can not do anything about it.

Imagine this:

try {
// this logical must be done, if not, the business logical will fail
vector.push_back(data); 
}catch(const std::exception& e) {
// program will not terminated, but what fail is fail, codes wrote here can not repair the failure. 
// The only thing you can do is report the condition to user: "sorry, your request failed"
}

Remember, when STL container can not allocate the memory which you demand is irrelative to actual size bigger than max size. max_size only describe a theoretical limitation but that limitation is impossible to reach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM