2014-07-18 23 views
6

Làm thế nào std :: string là nội bộ đại diện trong c + + 11 (libstdC++)?C++ 11 nội bộ std :: chuỗi đại diện (libstdC++)

Trong khi đào bên trong việc thực hiện, tôi thấy:

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

Tôi không hiểu những ý kiến ​​rất nhiều:

  • là std :: string ref tính? Làm sao? Tôi có nghĩa là _M_refcount không phải là một con trỏ, vì vậy nếu một chuỗi sửa đổi nó, người kia không thể nhìn thấy nó.
  • bộ đệm nằm ngay sau tiêu đề? Nếu đó là trường hợp tôi không thực sự hiểu tại sao.
+5

C++ 11 thực hiện tham chiếu tính các triển khai của 'std :: string' không tuân thủ. Có thể bạn đang xem xét việc triển khai libstdC++ vẫn sử dụng tính năng thực thi tính toán ref. – Praetorian

+0

Có, bạn đã đúng. – Borzh

+0

@Borzh - tự trả lời nếu bạn biết đồng ý Câu trả lời của Praetorian là đúng ... – NirMH

Trả lời

0

GCC đã di chuyển ra khỏi chuỗi được truy vấn theo tiêu chuẩn C++ 11, nhưng lưu ý rằng chương trình của bạn sẽ sử dụng nó như một phần của việc triển khai khả năng tương thích ABI.

Làm thế nào nó được refcounted

std::string không có thành viên _Rep_Base nhưng một con trỏ đến _Rep với _Rep kế thừa từ _Rep_Base

Đó là những gì được giải thích ở đây:

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

Bộ đệm nằm sau tiêu đề ...

Có, nhưng sau tiêu đề của đối tượng _Rep, và chuỗi của bạn chỉ có một con trỏ đến nó.

Các vấn đề liên quan