草庐IT

C++11 内部 std::string 表示 (libstdc++)

coder 2023-11-17 原文

std::string 在 c++11 (libstdc++) 中是如何内部表示的?

在深入研究实现时,我发现:

/*  A string looks like this:
 *
 *                                        [_Rep]
 *                                        _M_length
 *   [basic_string<char_type>]            _M_capacity
 *   _M_dataplus                          _M_refcount
 *   _M_p ---------------->               unnamed array of char_type
 *
 *  Where the _M_p points to the first character in the string, and
 *  you cast it to a pointer-to-_Rep and subtract 1 to get a
 *  pointer to the header.
 *
 *  This approach has the enormous advantage that a string object
 *  requires only one allocation.  All the ugliness is confined
 *  within a single %pair of inline functions, which each compile to
 *  a single @a add instruction: _Rep::_M_data(), and
 *  string::_M_rep(); and the allocation function which gets a
 *  block of raw bytes and with room enough and constructs a _Rep
 *  object at the front.
 *
 *  The reason you want _M_data pointing to the character %array and
 *  not the _Rep is so that the debugger can see the string
 *  contents. (Probably we should add a non-inline member to get
 *  the _Rep for the debugger to use, so users can check the actual
 *  string length.)
 *
 *  Note that the _Rep object is a POD so that you can have a
 *  static <em>empty string</em> _Rep object already @a constructed before
 *  static constructors have run.  The reference-count encoding is
 *  chosen so that a 0 indicates one reference, so you never try to
 *  destroy the empty-string _Rep object.
 */
  // _Rep: string representation
  //   Invariants:
  //   1. String really contains _M_length + 1 characters: due to 21.3.4
  //      must be kept null-terminated.
  //   2. _M_capacity >= _M_length
  //      Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
  //   3. _M_refcount has three states:
  //      -1: leaked, one reference, no ref-copies allowed, non-const.
  //       0: one reference, non-const.
  //     n>0: n + 1 references, operations require a lock, const.
  //   4. All fields==0 is an empty string, given the extra storage
  //      beyond-the-end for a null terminator; thus, the shared
  //      empty string representation needs no constructor.
  struct _Rep_base
  {
    size_type       _M_length;
    size_type       _M_capacity;
    _Atomic_word    _M_refcount;
  };

我不是很理解那些评论:

  • 是否计算了 std::string 引用?如何?我的意思是 _M_refcount 不是一个指针,所以如果一个字符串修改了它,另一个就看不到它。
  • 缓冲区紧跟在标题之后?如果是这样的话,我真的不明白为什么。

最佳答案

GCC 确实放弃了引用计数字符串以遵循 c++11 标准,但请注意,您的程序可能会将其用作 ABI 兼容性实现的一部分。

如何重新计算

std::string 没有 _Rep_Base 成员,而是指向 _Rep 的指针,_Rep 继承来自 _Rep_Base

这就是这里解释的内容:

 *  Where the _M_p points to the first character in the string, and
 *  you cast it to a pointer-to-_Rep and subtract 1 to get a
 *  pointer to the header.

缓冲区位于标题之后...

是的,但是在 _Rep 对象的 header 之后,您的字符串只有一个指向它的指针。

关于C++11 内部 std::string 表示 (libstdc++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24826936/

有关C++11 内部 std::string 表示 (libstdc++)的更多相关文章

  1. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  2. ruby - 字符串文字中的转义状态作为 `String#tr` 的参数 - 2

    对于作为String#tr参数的单引号字符串文字中反斜杠的转义状态,我觉得有些神秘。你能解释一下下面三个例子之间的对比吗?我特别不明白第二个。为了避免复杂化,我在这里使用了'd',在双引号中转义时不会改变含义("\d"="d")。'\\'.tr('\\','x')#=>"x"'\\'.tr('\\d','x')#=>"\\"'\\'.tr('\\\d','x')#=>"x" 最佳答案 在tr中转义tr的第一个参数非常类似于正则表达式中的括号字符分组。您可以在表达式的开头使用^来否定匹配(替换任何不匹配的内容)并使用例如a-f来匹配一

  3. ruby - 从 String#split 返回的零长度字符串 - 2

    在Ruby1.9.3(可能还有更早的版本,不确定)中,我试图弄清楚为什么Ruby的String#split方法会给我某些结果。我得到的结果似乎与我的预期相反。这是一个例子:"abcabc".split("b")#=>["a","ca","c"]"abcabc".split("a")#=>["","bc","bc"]"abcabc".split("c")#=>["ab","ab"]在这里,第一个示例返回的正是我所期望的。但在第二个示例中,我很困惑为什么#split返回零长度字符串作为返回数组的第一个值。这是什么原因呢?这是我所期望的:"abcabc".split("a")#=>["bc"

  4. Ruby - 如何将消息长度表示为 2 个二进制字节 - 2

    我正在使用Ruby,我正在与一个网络端点通信,该端点在发送消息本身之前需要格式化“header”。header中的第一个字段必须是消息长度,它被定义为网络字节顺序中的2二进制字节消息长度。比如我的消息长度是1024。如何将1024表示为二进制双字节? 最佳答案 Ruby(以及Perl和Python等)中字节整理的标准工具是pack和unpack。ruby的packisinArray.您的长度应该是两个字节长,并且按网络字节顺序排列,这听起来像是n格式说明符的工作:n|Integer|16-bitunsigned,network(bi

  5. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

  6. ruby - 安装libv8(3.11.8.13)出错,Bundler无法继续 - 2

    运行bundleinstall后出现此错误:Gem::Package::FormatError:nometadatafoundin/Users/jeanosorio/.rvm/gems/ruby-1.9.3-p286/cache/libv8-3.11.8.13-x86_64-darwin-12.gemAnerroroccurredwhileinstallinglibv8(3.11.8.13),andBundlercannotcontinue.Makesurethat`geminstalllibv8-v'3.11.8.13'`succeedsbeforebundling.我试试gemin

  7. ruby - 如何计算 Liquid 中的变量 +1 - 2

    我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我

  8. arrays - Ruby 数组 += vs 推送 - 2

    我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“

  9. ruby - 是否可以从也在该模块中的类内部调用模块函数 - 2

    在这段Ruby代码中:ModuleMClassC当我尝试运行时出现“'M:Module'的未定义方法'helper'”错误c=M::C.new("world")c.work但直接从另一个类调用M::helper("world")工作正常。类不能调用在定义它们的同一模块中定义的模块函数吗?除了将类移出模块外,还有其他解决方法吗? 最佳答案 为了调用M::helper,你需要将它定义为defself.helper;结束为了进行比较,请查看以下修改后的代码段中的helper和helper2moduleMclassC

  10. ruby - ri 有空文件 – Ubuntu 11.10, Ruby 1.9 - 2

    我正在运行Ubuntu11.10并像这样安装Ruby1.9:$sudoapt-getinstallruby1.9rubygems一切都运行良好,但ri似乎有空文档。ri告诉我文档是空的,我必须安装它们。我执行此操作是因为我读到它会有所帮助:$rdoc--all--ri现在,当我尝试打开任何文档时:$riArrayNothingknownaboutArray我搜索的其他所有内容都是一样的。 最佳答案 这个呢?apt-getinstallri1.8编辑或者试试这个:(非rvm)geminstallrdocrdoc-datardoc-da

随机推荐