3

After std::vector::resize() throws a std::bad_alloc exception, is the original data still valid and accessible in the std::vector object?

Does the answer hold for other allocators, e.g. if boost::interprocess::allocator is used as the allocator, and boost::interprocess::bad_alloc is thrown?

ezod
  • 7,261
  • 2
  • 24
  • 34
  • 2
    As per [cppreference](http://en.cppreference.com/w/cpp/container/vector/resize): *If an exception is thrown, this function has no effect (strong exception guarantee).* (I'll let someone with a copy of the standard answer the question) – Borgleader Nov 22 '17 at 15:24
  • I see a possible duplicate now (although the answer wording is specific to `push_back` and `push_front`): https://stackoverflow.com/questions/8899814/state-of-stdvector-after-stdbad-alloc – ezod Nov 22 '17 at 15:25

2 Answers2

6

std::vector::resize is exception safe.

If an exception is thrown, this function has no effect (strong exception guarantee).

Link to the exception specification. The specification doesn't mention any particular requirement on the allocator and must hold regardless of the allocator you provide.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
3

is the original data still valid and accessible in the std::vector object?

Yes, std::vector::resize has strong exception guarantee (except for the case mentioned as follows). §26.3.11.3/15 vector capacity [vector.capacity]:

If an exception is thrown other than by the move constructor of a non-CopyInsertable T there are no effects.

The guarantee has nothing to do with what kind of allocator is specified; so it's always true.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405