3 ms·
I like C (the language) a lot, but the C runtime library is a single big minefield (the mem*() and math.h functions are actually mostly fine, but everything els
by flohofwoe 5y ago
I like C (the language) a lot, but the C runtime library is a single big minefield (the mem*() and math.h functions are actually mostly fine, but everything else is terrible. Thankfully C doesn't rely as much on its standard libary as most other languages, and in many cases it actually is better to reinvent the wheel, because it's almost impossible to come up with worse APIs than the C standard library (and before "switch to C++": most areas of the C++ stdlib are even worse, because they allow the same memory corruption issues, but hide them more cleverly, which in turn makes debugging harder).
- formerly_proven 5y agoThe mem* functions are mostly fine because those operate on memory blocks and so everything needs to use explicit lengths. Most of the brokenness with the str* function family revolves around questions of who needs to check the length, will things be terminated correctly etc. and various ergonomic problems.
- tialaramex 5y agoWe can't blame the standard library for numerous infelicities in the language itself, and we likely shouldn't blame it for library designs that obviously fall out of those infelicities. Example #1 the array type looks useful, but oh, it's actually just coerced into a much less useful pointer You can have a C function signature that says it takes arrays of sixteen integers. But it doesn't! The compiler cheerfully ignores this and uses the function for any pointer to integers. Sixteen integers, or Zero, or Sixty, the same function executes. If you've used enough C you might just assume that's how it has to be. Nope. A Rust function declared for arrays of sixteen integers takes... arrays of exactly sixteen integers like you asked for. Not any other size. Example #2 implicit narrowing conversions everywhere This blew up in Linux recently, but C is OK with the idea of just assigning 64-bit values to 32-bit integers for example. The value doesn't fit in the narrower variable, but no problem just throw away the extra bits and it'll go in...
- flohofwoe 5y agoFor Example #1, C (but not C++) has a syntax for hinting the expected array size (note the warnings): https://www.godbolt.org/z/7bcd9xPr3 https://www.godbolt.org/z/7bcd9xPr3 For example #2, proper compilers have warnings for implicit conversions which would lose data, for instance: https://www.godbolt.org/z/vEP8ra9MP https://www.godbolt.org/z/vEP8ra9MP But for a proper solution to the array size problem you need array slice support built into the language, and this would be the first "opaque builtin struct type" in C (because slice types need either a pointer/pointer or pointer/size pair). At this point it's really better to switch to a different language.
- tialaramex 5y agoBut the compiler cheerfully still calls the function with a pointer instead, even if that pointer is in fact pointing at the too-short array. And yes, it really is better to switch, I've been doing so, and I am hopeful that the kernel can follow in due time. Aside: Matt Godbolt deserves some sort of award for Compiler Explorer. So much less awful to just link these examples in such conversations. It might well be as big a contribution to software engineering as git bisect or Tinderbox, or (really going back) Grace Hopper's "compiler". Yes Compiler Explorer seems "obvious" and people talked about things like this but did you build it? 'cos just talking about it doesn't make my life any better but building it does.
- pjmlp 5y agoIndeed, for Matt Godbolt, and the whole community that has added the remaining AOT languages, .NET is one of the few missing from Compiler Explorer, but it has a similar one at https://sharplab.io/ https://sharplab.io/ and there is one at http://shader-playground.timjones.io/ http://shader-playground.timjones.io/ for GPGPU shaders. Here is one talk from Matt Godbolt about how Compiler Explorer was born, CppCon 2019: Matt Godbolt “Compiler Explorer: Behind The Scenes” https://www.youtube.com/watch?v=kIoZDUd5DKw https://www.youtube.com/watch?v=kIoZDUd5DKw