std::vector and std::string reallocation strategy

前端 未结 4 1424
南方客
南方客 2021-01-17 23:03

What is the reallocation strategy used for std::string and std::vector in GCC\'s implementations?

I\'m interested in the specific strategy employed: When I append it

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-17 23:59

    std::vector has methods size and capacity. It should not be too difficult to write a simple program that determines the way memory allocations are being made. Strategies may change with each implementation, and even from version to version.

    One strategy I have seen is to use increasing increments, which is an adaptive strategy: give more food to the hungry to avoid frequent shuffles of the data. But the factor of the increase is open to discussion. A simple duplication may grow too fast.

    Later

    Curious myself, I wrote that program. Here is the output( g++ 4.3.3):

    capacity from 0 to 1 increased by 1 at size 1
    capacity from 1 to 2 increased by 1 at size 2
    capacity from 2 to 4 increased by 2 at size 3
    capacity from 4 to 8 increased by 4 at size 5
    capacity from 8 to 16 increased by 8 at size 9
    capacity from 16 to 32 increased by 16 at size 17
    capacity from 32 to 64 increased by 32 at size 33
    capacity from 64 to 128 increased by 64 at size 65
    capacity from 128 to 256 increased by 128 at size 129
    capacity from 256 to 512 increased by 256 at size 257
    capacity from 512 to 1024 increased by 512 at size 513
    capacity from 1024 to 2048 increased by 1024 at size 1025
    capacity from 2048 to 4096 increased by 2048 at size 2049
    capacity from 4096 to 8192 increased by 4096 at size 4097
    capacity from 8192 to 16384 increased by 8192 at size 8193
    capacity from 16384 to 32768 increased by 16384 at size 16385
    capacity from 32768 to 65536 increased by 32768 at size 32769
    capacity from 65536 to 131072 increased by 65536 at size 65537
    capacity from 131072 to 262144 increased by 131072 at size 131073
    capacity from 262144 to 524288 increased by 262144 at size 262145
    capacity from 524288 to 1048576 increased by 524288 at size 524289
    

    Using an initial allocation in the constructor results in the same progression, using the initial value rather than 1.

提交回复
热议问题