103 points · by fzliu · 3y ago · 96 comments
That said, maybe new C standard should come up with all-batteries included standard library upgrades. On second thought, imagine the compilers snafu.
I love C and I love Rust, for different reasons.
A proper import system would massively reduce the amount of wheel reinventing because then it would not be fiendishly difficult to actually reuse code in a coherent way. Only after that does it make sense to start quibbling about what is included where and by default.
Tagged union types would also be really, really nice.
It is almost a meme here, that, on any mention on C and C++, there will be people contributing comments: 'but Rust ..'.
C supports modularity since the 70s. You do not need a standard library with everything and the kitchen sink. The standard library is not the only component you're able to consume.
If there's a lesson from high level languages such as python and node.js, it's that being able to consume third party libraries is all anyone needs to get stuff done, and standard libraries bring convenience to the table but they aren't critical to get stuff done.
I think today we understand more about what a language needs to provide to enable modularity. One issue that comes up over and over with interoperating libraries is confusion around ownership. If you pass a char* to me, do I own it now? Is it my responsibility to free it? Or is it just a short-lived reference to data that's still yours? Garbage collected languages can kinda sorta sidestep these questions, at least with memory-use-after-free, and they're more likely to tolerate the costs of immutable data structures and defensive copies. But in C this question is critical, and we screw it up constantly, especially at the interface boundary between libraries. C++11 does much better, with first class support for moving ownership through a function call, and Rust goes further with lifetimes and the borrow checker.
Another point that's kind of in the weeds is the atomic memory model, which C didn't have until 2011 (and I think C++ gets most of the credit here for formalizing it). I'm still waiting for proper support for C11 atomics in MSVC. Multithreading has been ubiquitous for a while, and while it's possible for an application to decide that everything's going to be single-threaded, that's not an option for a portable library. Without standard atomics, you have to resort to platform-specific extensions to do basic stuff like initializing globals.
But likely neither of these is as big as letting void * represent any arbitrary construct, be it pointer or data structure or the universe itself. Finding references to void * in a codebase is akin to crossing the event horizon of a black hole.
Go literally hardcoded the same error-prone errno-like error handling, it just has some syntax sugar now. It still doesn’t compose, is unreadable and gives only the impression of properly handled error cases. It even fails to return a proper sum type, so now you get a possible important value and an error value as well.
What's the difference between returning a two element struct and a pair of value and error?
I never understood why "multiple return values" is a thing anyone should care about.
2. Without first-class support, it's very difficult to assert that the error is actually being checked. Consider:
int someVal = getSomeVal(...).v;
doSomethingWith(someVal);
vs. (int someVal, error e) = getSomeVal(...);
doSomethingWith(someVal);
Which is easier to catch at code-review time, or even at linter time?In most languages that don't support multiple return values, both problems are solvable with templates or local equivalent - see, for instance, absl::StatusOr for C++ (https://abseil.io/docs/cpp/guides/status).
I wonder if typeof in c23 has changed this at all. Previously there was no sense in defining an anonymous struct as a function's return type. You could do it, but those structs would not be compatible with anything. With typeof maybe that's no longer the case.
e.g. with clang 16 and gcc 13 at least this compiles with no warning and g() returns 3. But I'm not sure if this is intended by the standard or just happens to work.
struct { int a; int b; } f() {
return (typeof(f())){1,2};
}
int g() {
typeof(f()) x = f();
return x.a + x.b;
}
edit: though I suppose this just pushes the problem onto callers, since every function that does this now has a distinct return type that can only be referenced using typeof(yourfn).> In most languages that don't support multiple return values, both problems are solvable with templates or local equivalent
I don't really see the difference between tuples in C++ and multiple return values in Go (since you cite C++ as a language that doesn't have multiple return values).
val, err := do_something()
or auto [val, err] = do_something();
What's the real difference there? Syntactic sugar?one trivial possibility/option is to define in-out parameters, and just return errors:
<error-type/int/…> foo(T*, <params>)
will that not be ok ?For the same reason people care about multiple parameters. There's no reason that the data coming out of a function should be in any way more constrained than the data going into the function.
package hello
func good() (int, error) {
return 0, nil
}
func bad() *struct {
int
error
} {
return nil
}That article was from 2009. C11 didn't fix it. C18 didn't fix it. C23 didn't fix it.
Local variables are stack frame dependant. It makes perfect sense to make an array a pointer when using it out of the stack frame. It forces the programmer to actually think about what is going on underneath.
Edit: I just realised the article was written by D's Author! So maybe that is C's biggest mistake.
So ... yeah ... Walter's right, IMO.
C++ adds a number of its own flaws, but that's beside the point when discussing C's flaws from a perspective of knowing C++.
C has weaknesses. But it also has strengths, and those strengths mattered. It turns out they mattered more than the set of strengths that JOVIAL had.
I wonder if in several thousands year, mankind will have managed to get rid of C.
Zig in particular could scarcely do more to scream "I'm a replacement for C" without maybe asking WG14 to write "Just use Zig instead" in their next document.
As does Rust, though it makes for a bigger change.
This is possible for fixed-length arrays using 'myType (*myVar)[size]' as the argument type.
The annoying thing about this is that the array is just sitting there on the stack of the calling function. The size is completely known at compile time. But it gets thrown away as soon as you call a function.
That's what an array is. Fixed size. If we want to talk about a slice of some runtime determined amount of a thing then we need fat pointers, and C doesn't provide any fat pointer types so it's not a surprise to find it can't do slices.
There is no reason this shouldn’t be possible. It should not require fat pointers at all because the size information is known at compile time.
If the syntax is the problem, yeah, if you want first class data structures you might not like C.
The title of the article is “C’s biggest mistake.” If you’re going to rebut the article, do so. Otherwise your reply just comes off as “this is the way it is, deal with it!” which is a pretty shallow dismissal.
> using the int array1[2]; like this: arraytest(array1); causes array1 to automatically decay into an int .
> HOWEVER, if you take the address of array1 instead and call arraytest(&array1), you get completely different behavior!
> Now, it does NOT decay into an int
package main
func foo(a []byte) {
println(len(a))
}
func main() {
var b []byte
// cannot use &b (value of type *[]byte) as []byte value in argument to foo
foo(&b)
} package main
import "fmt"
func main() {
fmt.Printf("%q\n", "hello \x00 world")
}So C was [mostly] great, but today we have better options.
So, the desire and need for that flexibility was entirely warranted.