vector pointer locations guaranteed?

前端 未结 7 1662
无人及你
无人及你 2021-01-05 01:05

Suppose I have a vector of ints,

std::vector numbers;

that is populated with a bunch of values, then I say do this (where an ent

7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-05 01:27

    When you use a vector's resize() or reserve() function to increase the capacity of the vector, it may need to reallocate memory for the array-backing. If it does reallocate, the new memory will not be located at the same address, so the address stored in oneNumber will no longer point to the right place.

    Again, this depends on how many elements the vector is currently being used to store and the requested size. Depending on the specifics, the vector may be able to resize without reallocating, but you should definitely not assume that this will be the case.

提交回复
热议问题