Live data from Hacker News

My review of the C standard library in practice

nullprogram.com

51–60 of 94 posts

Re: My review of the C standard library in practice

#51

What are the alternatives to these libc functions? Making C even more bare bones than it already is makes no sense.

For many things there are much better versions of the functions available in the operating system. For example on macOS you really should be using Core Foundation for strings, dates, etc.

In a cross-platform application you can create a platform layer that wraps OS API usage and provides a POSIX fallback for the platforms you don't directly implement.

Re: My review of the C standard library in practice

#52

This just goes to show how hard it is to create a API to last 30 years. So much has changed since then, but libc needs to keep the same API. Things like Unicode and the internet (which brought the need for security to forefront) have come into popularity since then, but you can’t fix the old functions.

Well, that and also a showcase of just how BAD the design of the C stdlib string functions was, and continued to be with every iteration where someone introduced a new bad-and-poorly-thought-out API to replace the old bad-and-poorly-thought-out API.

It still boggles the mind how a function that leaves a C string potentially unterminated ever even made it into the standard...

Re: My review of the C standard library in practice

#53

This just goes to show how hard it is to create a API to last 30 years. So much has changed since then, but libc needs to keep the same API. Things like Unicode and the internet (which brought the need for security to forefront) have come into popularity since then, but you can’t fix the old functions.

Well, that and also a showcase of just how BAD the design of the C stdlib string functions was, and continued to be with every iteration where someone introduced a new bad-and-poorly-thought-out API to replace the old bad-and-poorly-thought-out API. It still boggles the mind how a function that leaves a C string potentially unterminated ever even made it into the standard...

>It still boggles the mind how a function that leaves a C string potentially unterminated ever even made it into the standard...

Easy to say with 50 years of hindsight. These are the pioneers we are talking about here. It boggles the mind how much they got right.

Re: My review of the C standard library in practice

#54

What are the alternatives to these libc functions? Making C even more bare bones than it already is makes no sense.

It's not as strange as you might think. Back in the classic Macintosh era (pre-OS X), it was quite common to write programs in C which essentially ignored libc. It was not a Unix system, so C's filesystem API made for an awkward fit, and the system's native callback-based IO was more efficient anyway. For similar reasons Mac programmers had little use for C's string functions or its allocator. Nor was there any terminal, so printf had little value.

I never spent any time in the early DOS/Windows world, but from what I heard similar coding patterns were found there.

Re: My review of the C standard library in practice

#56

I was unpleasantly surprised by how much locales are used for seemingly trivial things. It's even the case with the C++ iostreams stuff. I ended up writing my own functions in an 'ascii' namespace to avoid all this as much as possible. This [0] rant about locales and how they're nigh-on impossible to use correctly is quite amusing (slightly NSFW) [0] https://github.com/mpv-player/mpv/commit/1e70e82baa9193f6f02...

As a follow up, if anyone uses `boost::lexical_cast` a lot, try defining

    BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
for some performance speed-ups and code size reductions

Re: My review of the C standard library in practice

#57
post #53

Earlier quoted context omitted.

Well, that and also a showcase of just how BAD the design of the C stdlib string functions was, and continued to be with every iteration where someone introduced a new bad-and-poorly-thought-out API to replace the old bad-and-poorly-thought-out API. It still boggles the mind how a function that leaves a C string potentially unterminated ever even made it into the standard...

>It still boggles the mind how a function that leaves a C string potentially unterminated ever even made it into the standard... Easy to say with 50 years of hindsight. These are the pioneers we are talking about here. It boggles the mind how much they got right.

While strcpy is forgivable considering it came from the primordial soup, there is no excuse for strncpy since it was designed to solve the problems with strcpy. And the string library is full of gaffes like this.

Re: My review of the C standard library in practice

#59
post #38

Earlier quoted context omitted.

Still doesn't cover signals, only works if C11 threads are being used (it is all open if OS threads follow the same TLS mechanism), and those C89 and C99 code bases get nothing from it anyway.

In practice it works with major thread implementations even before C11, even though it doesn't say so in the standard. You can't call OS functions from a signal handler so I can't see why signals would matter. Have I missed something?

> You can't call OS functions from a signal handler

You can, as long as these functions are "async signal safe", see https://man7.org/linux/man-pages/man7/signal-safety.7.html for details.

Re: My review of the C standard library in practice

#60

This just goes to show how hard it is to create a API to last 30 years. So much has changed since then, but libc needs to keep the same API. Things like Unicode and the internet (which brought the need for security to forefront) have come into popularity since then, but you can’t fix the old functions.

Well, that and also a showcase of just how BAD the design of the C stdlib string functions was, and continued to be with every iteration where someone introduced a new bad-and-poorly-thought-out API to replace the old bad-and-poorly-thought-out API. It still boggles the mind how a function that leaves a C string potentially unterminated ever even made it into the standard...

WG14 has talked about looking into making string objects first class citizens in C and with it deprecating all the old narrow/wide string stuff.
Post reply on HN