gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 18:39:04 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com