Is string::size_type really big enough to hold any string?
I browsed through some threads about string::size_type
here and I do understand according to C++ standards that this size_type
guarantees enough allocation for all string usag开发者_开发知识库e.
I just find that hard to believe. What if I put the whole text of say C++ Primer 4th edition into a string? Or worse, what if I put infinite characters into a string? I just don't see how it can handle that.
string::size_type
is guaranteed to be large enough for all strings that the current implementation supports - not any string of any size. If your implementation supports strings up to e.g. 8 GB, size_type
can contain numbers up to 8 billion.
It doesn't guarantee enough allocation for all string usage. Quite the opposite: it limits the maximum length of a std::string
. Basically, it is the type that is used by the string implementation to hold its own length. This limitation is a bit artificial: usually, size_type
is large enough to hold an offset as large as the address space of the process - which of course can't be reached since you can never dedicate your entire address space to a single string.
(you actually can put the whole text of C++ Primer into a string.)
精彩评论