Documentation Buy Contact Blog

C++ std::string length

20 August 2014

Here is a weird 'feature' of std::strings, string.length() does not always equal strlen(string.c_str()). why is this? It's beacuse of the way std::strings handle the NULL character, which is to say they don't handle it at all.

In C, a string that you are used to dealing with is a series of characters terminated by a NULL character, eg "Crabs" becomes the series of characters 'C', 'r', 'a', 'b', 's', '\0'. Not properly handling the NULL character at the end of a string is probably the leading cause of bugs in a beginning C programmers career, and a possible source of many buffer overflow security issues later in a C programmers career.

Luckily for us C++ programmers, we have the std::string class that unlike a C string, has no terminator and stores its length is a seperate member variable. So this means that a null character can actually be a valid character in the string. For example, check the output of this code:

std::string sTemp("Crab\0Face", 9);
printf("%d %d\n", sTemp.length(), strlen(sTemp.c_str()));
printf("%s\n", sTemp.c_str()+5);

From this you can see that the NULL character is valid in std::stings, which can be helpful if you are doing weird things, but you need to be careful as all the classic C string handling functions will think that your string is shorter than it actually is.