Maximum length of a std::basic_string<_CharT> string
- by themoondothshine
Hey all,
I was wondering how one can fix an upper limit for the length of a string (in C++) for a given platform.
I scrutinized a lot of libraries, and most of them define it arbitrarily. The GNU C++ STL (the one with experimental C++0x features) has quite a definition:
size_t npos = size_t(-1); /*!< The maximum value that can be stored in a variable of type size_t */
size_t _S_max_len = ((npos - sizeof(_Rep_base))/sizeof(_CharT) - 1) / 4; /*!< Where _CharT is a template parameter; _Rep_base is a structure which encapsulates the allocated memory */
Here's how I understand the formula:
The size_t type must hold the count of units allocated to the string (where each unit is of type _CharT)
Theoretically, the maximum value that a variable of type size_t can take on is the total number of units of 1 byte (ie, of type char) that may be allocated
The previous value minus the overhead required to keep track of the allocated memory (_Rep_base) is therefore the maximum number of units in a string. Divide this value by sizeof(_CharT) as _CharT may require more than a byte
Subtract 1 from the previous value to account for a terminating character
Finally, that leave the division by 4. I have absolutely no idea why!
I looked at a lot of places for an explanation, but couldn't find a satisfactory one anywhere (that's why I've been trying to make up something for it! Please correct me if I'm wrong!!).