0

string s;

In C ++ string is a vector == pointer to start + length. But will there be a closing 0, as implicitly implied by Prat ?

class String{

private:

char * str; // pointer to string

int len; // line length

static int num_strings; // number of objects

static const int CINLIM = 80; // input limit for cin
} 

In other words , the string container does indeed contain ONLY a pointer to a char, and there is no closing zero ?

Влад
  • 31
  • 3
  • 4
    Are you asking about `std::string`, or your own `class String` here? We can't really say anything about your `class String` (it might or might not NUL-terminate); `std::string` requires the NUL in C++11 and later, but it wasn't a guarantee before then. – ShadowRanger Sep 09 '21 at 22:22
  • From C++11 onward, the memory buffer for `std::string` is required to be consecutive and null-terminated. Prior to C++11, there were no such requirements, but MOST implementations of `std::string` were, to keep the implementation of `std::string::c_str()` simple. – Remy Lebeau Sep 09 '21 at 22:22
  • @RemyLebeau To be precise, `str.c_str()` was always required to return pointer to contiguous null terminated string. What C++11 added was guarantee that `str[str.size()]` can be accessed and is a null terminator, and guarantees for `&str[0]` became identical to `c_str()`. The old libstdc++ string didn't write the null terminator until the call to `c_str()`. – eerorika Sep 09 '21 at 22:28
  • @eerorika well, to be precise, prior to C++11, `c_str()` was not even required to return a pointer to the string's memory buffer, it could return a pointer to a different buffer, so the buffer used by `operator[]` did not have to be consecutive or null-terminated. But IN PRACTICE, this was not the case though, and so C++11 simply standardize the practice that was already commonly in use, and added `data()`, and changed `c_str()` to match `data()`. – Remy Lebeau Sep 09 '21 at 22:36
  • @RemyLebeau It could be a separate buffer, but it would have to be owned by the string unless the language implementation also had garbage collection. Regarding practice, not being null terminated until the call to `c_str` was the case in practice in at least one popular implementation. – eerorika Sep 09 '21 at 22:39

0 Answers0