12

I've been playing around with the std::string_view library and I have been contemplating on changing a code base I have been working on to use std::string_view as much as possible. However, in many of the threads that I have read on the subject of when and where to use std::string_view instead of const std::string &. I have seen many answers say, "When you don't need a null terminated string." So as I began searching around the web for, "when do you need a null terminated string?" I haven't really come across any helpful answers on the subject.

I can think of an example of an external library that you will link to that requires a std::string. And in that case you would need a null terminated string since that library requires it. I guess another example would be if you need to modify the string itself, but then we wouldn't pass it by const & if we needed to modify it.

So when would you need to use a null terminated string?

Links that I have looked at:

  1. How exactly is std::string_view faster than const std::string&?
  2. When would I pass const& std::string instead of std::string_view?
  3. Why only string view?
  4. Is there sense in using const std::string& arguments in C++17?
Sailanarmo
  • 996
  • 8
  • 29
  • 5
    To interface with C APIs, mostly. – walnut Nov 22 '19 at 20:39
  • 1
    @uneven_mark can you provide an example of one? – Sailanarmo Nov 22 '19 at 20:43
  • The accepted answer to the fourth question you link seems like it answers this question. – François Andrieux Nov 22 '19 at 20:44
  • 1
    @Sailanarmo Most functions from e.g. POSIX or any other C library taking a `const char*` argument. eerorika's anwer has an example. – walnut Nov 22 '19 at 20:46
  • 1
    @FrançoisAndrieux so really the answer is, "As long as the API doesn't call for a null terminated string?" – Sailanarmo Nov 22 '19 at 20:46
  • It would be a mistake to standardise on string_view. You don't know when you will need to communicate with an external interface in future. If that happens, you will be forced to make a copy of the string which will eradicate any perceived gains in performance. – Richard Hodges Nov 25 '19 at 08:08

2 Answers2

10

When do you need a null terminated string?

You need a null terminated string whenever the API that you use says that you need it. This requirement is ubiquitous in C interfaces and not explicitly stated in some documentation. If a function argument is a char* (possibly to const), and there is no length argument, you should assume the requirement unless documentation says otherwise.

Let's take the function execve (from POSIX standard) as an example:

int execve(const char *pathname, char *const argv[], char *const envp[]);

If you pass a non-null terminated argument as pathname, then the behaviour of your program will be undefined.

eerorika
  • 181,943
  • 10
  • 144
  • 256
  • So in this case, the pathname, were it to be a `std::string`, would be executed like `execve(pathname.c_str(),...,...)`? – Sailanarmo Nov 22 '19 at 20:49
6

It's actually pretty easy to know. If you are calling a function that just takes a c-string (char*/const char*), then you need a null terminated string as that is the only way to know where then end of the string is.

If you instead have a function that takes a char*/const char* plus the size, or just two pointers marking the beginning and end of the data, then you don't need a null terminated string since you have/can get the string size without iterating to a null terminator.

NathanOliver
  • 150,499
  • 26
  • 240
  • 331