Why null-terminated strings? Or: null-terminated vs. characters + length storage

后端 未结 10 1261
-上瘾入骨i
-上瘾入骨i 2020-12-23 17:29

I\'m writing a language interpreter in C, and my string type contains a length attribute, like so:

struct String
{
    char* charac         


        
相关标签:
10条回答
  • 2020-12-23 17:43

    From Joel's Back to Basics:

    Why do C strings work this way? It's because the PDP-7 microprocessor, on which UNIX and the C programming language were invented, had an ASCIZ string type. ASCIZ meant "ASCII with a Z (zero) at the end."

    Is this the only way to store strings? No, in fact, it's one of the worst ways to store strings. For non-trivial programs, APIs, operating systems, class libraries, you should avoid ASCIZ strings like the plague.

    0 讨论(0)
  • 2020-12-23 17:48

    One benefit is that with null-termination any tail of a null-terminated string is also a null-terminated string. If you need to pass a substring starting with Nth character (provided there's no buffer overrun) into some string-handling function - no problem, just pass the offseeted address there. When storing size in some other way you would need to construct a new string.

    0 讨论(0)
  • 2020-12-23 17:49

    Just throwing out some hypotheticals:

    • there's no way to get a "wrong" implementation of null terminated strings. A standardized struct however could have vendor-specific implementations.
    • no structs are required. Null terminated strings are "built-in" so to speak, by virtue of being a special case of a char*.
    0 讨论(0)
  • 2020-12-23 17:54

    I think main reason is that standard says nothing concrete about size of any type other than char. But sizeof(char) = 1 and that is definitely not enough for string size.

    0 讨论(0)
提交回复
热议问题