NULL Strings

The standard library string c_str() method returns "a null-terminated array of characters representing the string's contents" [2]. It is important to note that it does not qualify this definition with whether the represented string is empty, so the implementation of the string may store a null pointer when an instance is in an "empty" state. But when called in that state it must still return a valid string; obviously it should return the empty string. A typical implementation would be like the following:

template <...>
inline char_type const *string_class<...>::c_str() const
{
  return m_buffer != 0 ? m_buffer : "";
}
With C-strings, ie., char const *, an empty string can come in two forms, the empty string (which is a pointer to an array of one character, with value 0), or it can be the null string (which is the null pointer). This is a serious disparity. On many architectures, de-referencing the null pointer causes an access violation, and it is in all cases undefined, so it is something we want to avoid.

The strategy I chose for the string access shims was to implement c_str_ptr to match the semantics of c_str(), hence it never returns a null pointer. What this means is that any algorithm that uses the c_str_ptr shims can rely on the fact that it will never be presented with a null string.

However, some uses of shims might require a NULL pointer to be returned when the string is empty. This is supported by the shim c_str_ptr_null. Each c_str_ptr() shim in the STLSoft libraries has a matching c_str_ptr_null() shim, some of which are shown in Listing 11.