3 ms·
It's not really complacency: it's that the standard library is intentionally minimalistic to maintain portability and backwards compatibility. If you want sensi
by dangerbird2 6y ago
It's not really complacency: it's that the standard library is intentionally minimalistic to maintain portability and backwards compatibility. If you want sensible string handling, it's usually best to use a high level utility library like GLib(https://developer.gnome.org/glib/stable/ https://developer.gnome.org/glib/stable/) or Apache Portable Runtime(http://apr.apache.org/ http://apr.apache.org/), or roll your own safe string type (preferably non-null terminating)
- yxhuvud 6y agoNo, if you want sensible string handling, the sane choice is usually to choose to use a language that is not C. Not always, but definitely usually.
- IgorPartola 6y agoIt’s not hard to have strings like you do in other languages in C. It is hard when you treat char foo[] as if it was a string object like you have in JavaScript or Java or Python. C strings are just chunks of memory terminated by \0. They can still be mildly useful that way but if you actually want to do string operations you need to use a library designed for the problem (variable length, storing length with the object, Unicode support, etc.). Problem is that most people don’t start with such a library so they end up doing the hard work themselves in an ad hoc manner. You can’t fuck up String(“Hello “) + String(“world”) but you can definitely fuck up strcat(buf, “Hello “); strcat(buf, “world”);.
- Ar-Curunir 6y agothere's nothing inherently unportable about strings though.
- ironmagma 6y agoWhy do you need backward compatibility with a compiled language? Other languages like Rust and JavaScript (even) avoid that with a pragma tag on the source.
- hctaw 6y agoBecause not everything is recompiled from source. That's why stable ABIs need to exist.
- ironmagma 6y agoGood point, thanks. Could the headers contain the pragmas?
- hctaw 6y agoThat assumes you have a header, which only exists at compile time for the developer. The running program knows nothing about it.
- ironmagma 6y agoWhy would a program need to know (e.g.) the details of what system calls or stdlib functions that a procedure it invokes uses? Aren’t C functions pretty well separated from each other except for the odd signal handler and assuming a stable ABI? In my view most of the issues with C are semantics within the function blocks.
- rightbyte 6y agoThe parameters and return value is not in the object files.
- hctaw 6y agoThe program doesn't "know" anything. The executable has a header used by the loader that tells it to use libc. If the libc version does not contain the symbols that the program expects or the symbols do not have compatible definitions (meaning identical function signature and ABI, including struct layout) the program will probably crash. If the program links against any shared libraries they'll use the same version of libc that's loaded with the executable. There are ways around this that are varying degrees of acceptable. Versioning libc itself is outside the scope of the language, since it really depends on how the system linkers and loaders are implemented.