c++vectormax-size

C++ vector max_size();


On 32 bit System.

  1. std::vector<char>::max_size() returns 232-1, size of char — 1 byte
  2. std::vector<int>::max_size() returns 230-1, size of int — 4 byte
  3. std::vector<double>::max_size() returns 229-1, size of double — 8 byte

can anyone tell me max_size() depends on what?

and what will be the return value of max_size() if it runs on 64 bit system.


Solution

  • Simply get the answer by

    std::vector<dataType> v;
    std::cout << v.max_size();
    

    Or we can get the answer by (2^nativePointerBitWidth)/sizeof(dataType) - 1. For example, on a 64 bit system, long long is (typically) 8 bytes wide, so we have (2^64)/8 - 1 == 2305843009213693951.