3 ms·
This might be a dumb question, but using this + clang bounds-safety, whats the difference between this and something like Zig or Odin. What do you think C woul
by valorzard 5mo ago
This might be a dumb question, but using this + clang bounds-safety, whats the difference between this and something like Zig or Odin.
What do you think C would need in order to reach the user experience of those languages?
- EPWN3D 5mo ago> This might be a dumb question, but using this + clang bounds-safety, whats the difference between this and something like Zig or Odin. I really need to learn more about Zig, but from what I know, there are still worlds of possibilities that a modern, well-designed language offers over something like lib0xc. Zig's ability to evaluate any expression at compile-time is one such example. But generally, lib0xc gives you bounds-safety everywhere it can. Languages like Zig and Rust give you type-safety to their own degrees, which I think is a superset. > What do you think C would need in order to reach the user experience of those languages? Not really having direct user experience, it's hard for me to say. But if I what I can give you is a list of features that would make large parts of lib0xc irrelevant: 1. Protocols/traits 2. Allocating from a caller's stack frame (think, returning the result of `alloca` to the caller) 3. printf format specifiers for stdint.h types and for octet strings 4. Ability to express function parameter lists as structures 5. New sprintf family that returns a value which is always less than or equal to the size passed (no negative values) Basically, I think that the C standard should be working aggressively to cut down on the use cases for heap allocation and `void *`. And I think that the bounds safety annotations should become first-class language features.
- anthk 5mo agoWouldn't the last case (void *) hurt embedded C development, or retrogaming with direct memory access and pointers?
- debo_ 5mo agoThey said "cut down", not "eliminate."
- EPWN3D 5mo agoNot really. You'd still be able to address memory as bytes. The problem with void * is that it strips all bounds information by its nature. Most of the time when you're passing a void * without an associated length (e.g. context pointers, objects that you pinky-swear are of a certain type), it indicates a failure in the language. That's the stuff I think needs to be eliminated.
- anthk 5mo agoHave a look on libre C SDK's for the GBA and read about how some data it's set. Ditto with another set of archs where some simple C89 it's being ported.
- onlyrealcuzzo 5mo ago> I really need to learn more about Zig, but from what I know, there are still worlds of possibilities that a modern, well-designed language offers over something like lib0xc. Doesn't Apple have a nice `defer { }` block for cleanup? Did you include that in lib0xc? I didn't see in on your README.
- akoboldfrying 5mo agoIn C++ you can implement such a thing using destructors, which are guaranteed to run in reverse order on scope exit even in the presence of exceptions. Alexei Alexandrescu's Scopeguard did this (in the 90s I think, long before C++11). But in standard C, there's no mechanism that this could be attached to (especially if you want to use "C exceptions", a.k.a. setjmp()/longjmp()). Maybe the compilers they support all have non-standard extensions that allow something like this though?
- shakna 5mo agoYes, because all compilers support a non-standard defer mechanism, its now being considered for inclusion into standard C. [0] And that suggested defer standard, is already available from GCC 9 and clang 22. [0] https://www.open-std.org/JTC1/SC22/WG14/www/docs/n3734.pdf https://www.open-std.org/JTC1/SC22/WG14/www/docs/n3734.pdf
- lyrie 5mo ago[flagged]
- EPWN3D 5mo agoI think defer has been included in the next round of working group proposals for C2y, but I don't think Apple's clang has it. Maybe it's there as a language extension and I just didn't see it. What lib0xc has is some cleanup attributes that you can apply to variables to e.g. automatically free a heap allocation or close a file descriptor, at end of scope. Personally, I like variable annotations much more than defer for these uses, but they accomplish the same thing. I've also found that using those attributes inherently pushes your code to make ownership more explicit. I personally stopped being terrified of double-pointers and started using them for ownership transfers, which eliminates a large class of bugs.
- saidnooneever 5mo agoWhy not pick a different language if you want different features? Why does C specifically need to change, if there are already Zig, Rust etc.? Why Must C be safe, rather than people writing safer code in it or transfering to other languages if they cannot be bothered?
- jeremie_strand 5mo ago[flagged]
- EPWN3D 5mo agoC does not need to be completely safe. But it should be safer by default than it currently is. And I think that "it's a low level language, you just need to be smarter" is too often a cover story for bad design decisions. I don't think that the choices should be "self-driving cars" and "cars with no seatbelts, airbags, or crumple zones" with nothing in between.
- up2isomorphism 5mo agoBecause it is C not Zig/Odin. The mental/ecosystem cost to use a new language is way way under-estimated in most cases.
- deleted 5mo ago[deleted]
- josephg 5mo ago> What do you think C would need in order to reach the user experience of those languages? - A better, cross platform build system. - Generics - Ergonomic compile time metaprogramming (zig's comptime, rust's macro system, etc) - Closures - or at least lambdas. - The ability to return multiple values - eg tuples - Sum types. - A built in way to return an error | value from a function - Defer / Rust's Drop C is anaemic.