Why is the max size of a std :: string object is 4611686018427387897?

Viewed 146

I have code where I output the max length of a std::string object:

string s;
cout<<s.max_size()<<endl;
cout << INT_MAX << endl;
cout<<LLONG_MAX<<endl;

Output :

4611686018427387897
2147483647
9223372036854775807

If it's not difficult, please explain why the maximum size of std::string is 4611686018427387897. This is approximately half of LLONG_MAX.

1 Answers

It depends on how basic_string is implemented in the standard library that you are using. Different implementations may "use" a bit from the size of the string for other purposes (for example, a flag that says "this is a short string").

Related