Hacker News new | past | comments | ask | show | jobs | submit login

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.



Unlike 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.


> 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

You can also wrap a const char* in a string_view which has a length.


You 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?


String literals are char arrays, so it seems reasonable to be able to not resize them…


The 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.)


I 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.



This is exactly the kind of thing I was thinking about. Great!




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: