Re:Show HN
by jstimpfle on Saturday December 03, 2022 @09:41AM
(#33843792)
"Strings" are quite an abstract concept. In general they are understood as a linear sequence of characters. But there are a number of ways to represent them, the simplest of which is a contiguous memory allocation. There are also different ways to do the necessary memory management (e.g. allocate statically at compile time vs dynamically at run time).
Maybe motivated from higher-level dynamic or managed language, it is thought that strings should always be allocated dynamically, and support operations like string-append with automatic reallocation if the currently allocated memory isn't enough to store the new string.
In practice, that's not true at all - unless you are in a domain where lots of small intermediate strings are generated. This is pretty inefficient anyway and there is probably no point to use C in this case.
By far most strings in most domains are either completely static (use string literals), or are created once in a sequence of append operations and then never changed again. I get by, doing many different things from GUI apps to networking to parsers and interpreters, without any sophisticated string type. All I do is define some printf-like APIs to do logging, for examples. Those typically just use a fixed size buffer for the formatting, and then flush that buffer to e.g. stderr. or flush it to a dynamically allocated memory buffer, but there almost never is a need to reallocate that string later.
Maybe motivated from higher-level dynamic or managed language, it is thought that strings should always be allocated dynamically, and support operations like string-append with automatic reallocation if the currently allocated memory isn't enough to store the new string.
In practice, that's not true at all - unless you are in a domain where lots of small intermediate strings are generated. This is pretty inefficient anyway and there is probably no point to use C in this case.
By far most strings in most domains are either completely static (use string literals), or are created once in a sequence of append operations and then never changed again. I get by, doing many different things from GUI apps to networking to parsers and interpreters, without any sophisticated string type. All I do is define some printf-like APIs to do logging, for examples. Those typically just use a fixed size buffer for the formatting, and then flush that buffer to e.g. stderr. or flush it to a dynamically allocated memory buffer, but there almost never is a need to reallocate that string later.