1

Topic: std:: string

Good afternoon dear programmers.
If not difficult share expertize about that as it is arranged std:: string.
Whether correctly I understand that by default there exists certain Allocator (sewn up in a template)
Which defines where will be  storage towards the line (in  or on a stack).
But is still certain Small String Optimizator which  on a stack if a line small.
I will be grateful for a detailed explanation as all this kitchen works.
P.S.: if there is no desire to me that that to explain and it wants to send me to read the documentation - I ask not to become angry and simply to ignore a subject.
In advance many thanks.

2

Re: std:: string

Simeon. ;
If to remove insignificant details:

class string
{
char* data;
size_t len;
char short_data [N];
};

If the line is less N that it is allocated directly in object in an array short_data [], and data specifies on short_data and if it is more the buffer forms in a heap with the help new char [len], and data specifies in it.

3

Re: std:: string

IN basic_string.h:

139 _Alloc_hider _M_dataplus;
140 size_type _M_string_length;
141
142 enum {_S_local_capacity = 15 / sizeof (_CharT)};
143
144 union
145 {
146 _CharT _M_local_buf [_S_local_capacity + 1];
147 size_type _M_allocated_capacity;
148};

Accordingly at attempt to appropriate a short line there is something like

550 basic_string (basic_string&& __ str, const _Alloc& __ a)
551 noexcept (_Alloc_traits:: _S_always_equal ())
552: _M_dataplus (_M_local_data (), __ a)
553 {
554 if (__ str. _ M_is_local ())
555 {
556 traits_type:: copy (_M_local_buf, __ str. _ M_local_buf;
557 _S_local_capacity + 1);
558 _M_length (__ str.length ());
559 __ str. _ M_set_length (0);
560}

4

Re: std:: string

Aha understood, thanks

5

Re: std:: string

.  wrote:

good afternoon dear programmers.
If not difficult share expertize about that as it is arranged std:: string.
Whether correctly I understand that by default there exists certain Allocator (sewn up in a template)
Which defines where will be  storage towards the line (in  or on a stack).
But is still certain Small String Optimizator which  on a stack if a line small.
I will be grateful for a detailed explanation as all this kitchen works.
P.S.: if there is no desire to me that that to explain and it wants to send me to read the documentation - I ask not to become angry and simply to ignore a subject.
In advance many thanks.

It is impossible for string  storage on a stack...

6

Re: std:: string

MasterZiv;
And experts above write other

7

Re: std:: string

.  wrote:

MasterZiv;
And experts above write other

They write that characters of a line can be allocated in object of a line. And the object can be thus both on a heap and on the stack.

8

Re: std:: string

What we will do with c_str ()?

9

Re: std:: string

alex_k wrote:

They write that characters of a line can be allocated in object of a line. And the object can be thus both on a heap and on the stack.

Actually  allocating on a stack too it is possible. It is just necessary to understand consequences))

mayton wrote:

That we will do with c_str ()?

With it , it is necessary to do nothing all))

10

Re: std:: string

mayton wrote:

That we will do with c_str ()?

To change on data () smile