3 ms·
> The flaw isn't the null termination but the lack of encapsulation. > Null-terminated strings are actually a very good choice of string representation that is
by talaketu 7y ago
> The flaw isn't the null termination but the lack of encapsulation.
> Null-terminated strings are actually a very good choice of string representation that is to be directly manipulated by programs, instead of just through opaque handles via library functions.
You seem to be having it both ways.
As I see it, "NULL" is a valid ascii character, but not a valid element of a C string - except as a terminator external to the string of characters being represented. It's fundamentally broken as an abstraction for managing variable length strings of ASCII. It is incapable of representing all strings of ASCII characters.
- kazinator 7y agoYou're arguing about requirements. Of course when Thompson and Ritchie went for null terminated strings, they knew that NUL would be excluded. That's the specification at the requirement level; it's not a "oh crap, we unwittingly excluded NUL from strings" bug. There are 32 control characters, and DEL. The purpose of all those characters is to carry some kind of meaning. The designers of the null-terminated string decided that the meaning of NUL will be reserved for that termination. That still leaves 31 control characters and DEL which can be stored in a string, which programmers can use for other meanings. That turns out to be plenty. The "unreasonable effectiveness" and ubiquity of null terminated strings shows that the world is perfectly capable of getting along without NUL in the middle of a string. Basically, the intuition that we can steal NUL for string termination was right (and to some extent self-fulfilling, due to the subsequent proliferation of the convention). The idea that all possible code points of a character set are valid characters that must be stored in a string is outdated in the age of Unicode, which contains codes which are not character code points. Null terminated strings are now the representation of a path name every major OS that has any non-negligible installed base. The choices leading to this situation weren't made by complete idiots. And, by the way, NUL is the ASCII character; NULL is the pointer constant in C.
- kazinator 7y agoI apologize for that last remark. In fact, the first ASCII document from 1963 uses NULL! http://worldpowersystems.com/ARCHIVE/codes/X3.4-1963/page5.JPG http://worldpowersystems.com/ARCHIVE/codes/X3.4-1963/page5.J... This got shortened to NUL in 1965.