C++ vector max_size();

前端 未结 3 1219

On 32 bit System.

  1. std::vector::max_size() returns 232-1, size of char — 1 byte
  2. std::vecto
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-31 05:38

    max_size() is the theoretical maximum number of items that could be put in your vector. On a 32-bit system, you could in theory allocate 4Gb == 2^32 which is 2^32 char values, 2^30 int values or 2^29 double values. It would appear that your implementation is using that value, but subtracting 1.

    Of course, you could never really allocate a vector that big on a 32-bit system; you'll run out of memory long before then.

    There is no requirement on what value max_size() returns other than that you cannot allocate a vector bigger than that. On a 64-bit system it might return 2^64-1 for char, or it might return a smaller value because the system only has a limited memory space. 64-bit PCs are often limited to a 48-bit address space anyway.

提交回复
热议问题