std :: basic_string< _CharT>的最大长度串 [英] Maximum length of a std::basic_string<_CharT> string

查看:137
本文介绍了std :: basic_string< _CharT>的最大长度串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何解决给定平台的字符串长度的上限(在C ++中)。

I was wondering how one can fix an upper limit for the length of a string (in C++) for a given platform.

我仔细检查了很多库,并且大多数任意定义它。 GNU C ++ STL(具有实验C ++ 0x功能的那个)有相当定义:

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 */

这是我如何理解公式:


  • size_t类型必须包含分配给字符串的单位计数(每个单位的类型为_CharT)

  • 理论上,size_t类型的变量可以采用的最大值是可以分配的1个字节(即,char类型)的单位总数。

  • 上一个值减去跟踪分配的内存(_Rep_base)所需的开销因此是字符串中的最大单位数。将此值除以sizeof(_CharT),因为_CharT可能需要超过一个字节

  • 从上一个值减去1以计算终止字符

  • 最后,离开了除以4。我完全不知道为什么!

  • 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!!).

推荐答案

来自GCC 4.3.4状态的basic_string.h中的注释:

The comments in basic_string.h from GCC 4.3.4 state:

    // The maximum number of individual char_type elements of an
    // individual string is determined by _S_max_size. This is the
    // value that will be returned by max_size().  (Whereas npos
    // is the maximum number of bytes the allocator can allocate.)
    // If one was to divvy up the theoretical largest size string,
    // with a terminating character and m _CharT elements, it'd
    // look like this:
    // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
    // Solving for m:
    // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
    // In addition, this implementation quarters this amount.

特别注意最后一行此外,

In particular, note the last line, "In addition, this implementation quarters this amount." I take that to mean that the division by four is in fact entirely arbitrary.

我试图在这里找到更多信息。

I tried to find more information in the checkin log for basic_string.h, but it only goes back to October 5, 2000, and this comment was already present as shown in that revision, and I'm not familiar enough with that code base to know where the file might have lived in the source tree before it was moved to its current location.

这篇关于std :: basic_string&lt; _CharT&gt;的最大长度串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆