Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does string::capacity/reserve() count terminating null?

It's not obvious from cppref's description on capacity() and reserve() whether terminating null character is counted.

like image 561
Lingxi Avatar asked Mar 05 '23 09:03

Lingxi


1 Answers

The standard states that:

In all cases, size() <= capacity().

And size() does not include the terminating null.

Since it is possible that size() equals capacity(), in that case it would mean that capacity() also does not count the terminating null.

Note that in C++11 and later, mystring.c_str() is equivalent to mystring.data() is equivalent to &mystring[0], and mystring[mystring.size()] is guaranteed to be '\0'.

Check this Demo.

like image 115
P.W Avatar answered Mar 15 '23 13:03

P.W