Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do you need a null terminated string in a read-only scenario?

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?
like image 790
Sailanarmo Avatar asked Nov 22 '19 20:11

Sailanarmo


People also ask

Why do strings need to be null-terminated?

Character encodings Null-terminated strings require that the encoding does not use a zero byte (0x00) anywhere; therefore it is not possible to store every possible ASCII or UTF-8 string. However, it is common to store the subset of ASCII or UTF-8 – every character except NUL – in null-terminated strings.

What happens if you dont null terminate a string?

If you use a non-null-terminated char sequence as a string, C functions will just keep going. It's the '\0' that tells them to stop. So, whatever happens to be in memory after the sequence will be taken as part of the string.

Is a string always null-terminated?

Yes, the CString is always null terminated.

What is a null terminator in a string?

The null character indicates the end of the string. Such strings are called null-terminated strings. The null terminator of a multibyte string consists of one byte whose value is 0. The null terminator of a wide-character string consists of one gl_wchar_t character whose value is 0.

What is a null terminated string?

The null terminated strings are basically a sequence of characters, and the last element is one null character (denoted by ‘0’). When we write some string using double quotes (“…”), then it is converted into null terminated strings by the compiler.

Are string literals null terminated in Java?

String literals like "Hello World!" are null-terminated, but char arrays are not automatically null terminated. The general principle I've always taken is to be extra cautious and assign '\0' to the the end of the string unless that causes a performance problem. In those cases, I'm extra careful about which library functions I use.

Is Hello World a null-terminated string?

String literals like "Hello World!" are null-terminated, but char arrays are not automatically null terminated. The general principle I've always taken is to be extra cautious and assign '\0' to the the end of the string unless that causes a performance problem.

Is it necessary to add a null terminator to scanf?

No, because when you call scanf, strcpy (except for strncpy where you need to manually put zero if it exceeds the size), it copies the null terminator for you. Is it good to do it anyways? Not really, it doesn't really help the problem of bufferoverflow since those function will go over the size of the buffer anyways.


2 Answers

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.

like image 137
eerorika Avatar answered Oct 19 '22 00:10

eerorika


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.

like image 37
NathanOliver Avatar answered Oct 19 '22 01:10

NathanOliver