3 ms·
I've always wondered why the C standard didn't just bite the bullet and added a string object, with a malloc-backed expanding string inside.
by hyperman1 4y ago
I've always wondered why the C standard didn't just bite the bullet and added a string object, with a malloc-backed expanding string inside.
- Ekaros 4y agoI think there is sufficient membership in standard committee that doesn't want such "automated" features specially when touch memory allocation and freeing. And I too find it somewhat antithetical to C as language. Still. Why isn't there a good quality library or set of libraries that would cover these features is my question.
- tialaramex 4y agoUnlike C++ the C language takes its freestanding mode seriously. In this mode there's no malloc() and so your string type can't exist. But today in freestanding I can write "ERROR" and get a string literal. So that would mean either you abolish string literals in freestanding (making C worse) or you have a separate type for these string literals from the type for a expandable string. Now, Rust pulls off the latter in my view fairly elegantly, but that takes a lot of sophisticated type features in the language including a DeRef and several AsRef implementations. In Rust these are general features open to other types, C could special case them instead, but that's still a lot of engineering for a small feature. The result is definitely not the elegant language which can be described in a slim book. Maybe it's better anyway, but it's not C.
- saagarjha 4y agoString literals are char arrays, so it seems reasonable to be able to not resize them…
- 323 4y ago> Unlike C++ the C language takes its freestanding mode seriously You can easily implement a stack allocated C++ string: https://forums.4fips.com/viewtopic.php?f=3&t=1075 https://forums.4fips.com/viewtopic.php?f=3&t=1075 You can also wrap a const char* in a string_view which has a length.
- pjmlp 4y agoYou mean the lightly specified section of ISO C standard regarding freestanding use, or the compiler extensions most people ignore aren't part of the standard?
- kzrdude 4y agoThe rules around const don't compose with pointers inside structs, which makes this more complicated than you'd like. (I guess, just one struct for mutable and one for view of the string would work.)