5 ms·
I fear all of this is dancing around the nasty core of the problem: generic writing to C style strings can't be done without extra information. You can't writ
by hyperman1 3y ago
I fear all of this is dancing around the nasty core of the problem: generic writing to C style strings can't be done without extra information. You can't write stuff to memory without negotiating how much room is needed and available, and optionally moving the string. Silent truncation will cause bugs. Buffer overflow even more.
Fixing this now is hard: Writing to 0-ended strings require manually tracking lengths. Expanding a string without allowing malloc is misery.
The only way out I see is basically starting from zero: ISO C should define an API with a (pointer,current length, max length) struct at its core, pointer pointing to a 0-terminated C string. You can read it, but changing it requires using functions that can error out and/or malloc more memory. There are already multiple libs like this, but C has none. If the struct would be ABI, non-C programming languages can pass strings between them.
- samtho 3y agoC had the opportunity to include this but they did not. It is my understanding that they wanted to design everything in C as inherent to the language, rather than magic types, especially a struct. There is an elegance in the notion that a string is just an array of characters. If I’m working with a significant amount of strings in C, I can keep track of lengths, not a huge deal.
- jstimpfle 3y agoExactly this. There are no literals in C that create composite types. There are no composite types inherent to the language. All these types are defined in (system) includes. And zero-terminated strings are not strictly worse than other length-prefixed string forms. They save some space -- sure, less relevant today -- as well as provide an in-band termination signal -- which is hacky, again sure, but it is convenient when looking at a hex dump for example.
- nicoburns 3y ago> There are no literals in C that create composite types Float/double literals do
- jstimpfle 3y agohow so?
- jacquesm 3y agoBecause a float contains bit fields including sign of the exponent, sign, mantissa, sign of the mantissa. It's a bit of a pedantic argument but technically it makes sense.
- jstimpfle 3y agoYou could call them composite in that sense, but in C, composite types are types that are composed of other C types (structs, unions, arrays, ... functions? Not 100% sure of the specifics). Also, the representations of floats and doubles isn't precisely specified, at least IEEE754 is not a strict requirement (not sure about the technical implications from what's actually specified).
- jacquesm 3y agoI'm not the one making the argument, merely explaining why the GGP may have chosen this example.
- jstimpfle 3y agoWasn't implying that! :)
- nicoburns 3y agoI'm not really sure I get why the distinction between composite and non-composite types is important if the only difference is that you can't easily access sub-parts of the non-composite types.
- jstimpfle 3y agoI guess we can cook up some arguments about language purity and orthogonality. Introducing literals that create composite types might indeed create some difficulties for existing compilers that want to follow the latest standard.
- abbeyj 3y agoThere are literals that create composite types, since C99: https://en.cppreference.com/w/c/language/compound_literal https://en.cppreference.com/w/c/language/compound_literal
- jacquesm 3y agoAn early version of C didn't have structs, the initial attempt to get the OS off the ground failed, and after adding structs it worked. Structs are just syntactic sugar over memory offsets relative to a base pointer, a construct for which many CPUs include primitives.
- imtringued 3y agoC is lots of magic and quirkiness. This reminds me. From a spec/design perspective Ceylon was the cleanest language I know. Almost everything, including a lot of the keywords were actually defined in the standard library. The fact that Integer was actually a Java int behind the scenes was just a compiler implementation detail. There was very little "magic". If you wanted to know how something in the language worked you could just look at the standard library.
- jacquesm 3y agoYou can't even really assume that strings are writable, they might well be in ROM on an embedded device.