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:
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.
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.
Yes, the CString is always null terminated.
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With