Hacker News new | ask | show | jobs
by Gibbon1 1829 days ago
I swear part of the problem is with C there is a cargo cult prohibition against passing small structs by value.
4 comments

The cargo cult goes beyond that.

The belief it was created alongside UNIX from the start, when it was used to port UNIX V4 into high level language.

Micro-optimizing each line of code as it is written, "because it is fast", without even bothering to use a profiler.

Even though lint was created alongside C to fix already known programmer faults using the language, in 1979, the belief that only bad programmers need such kind of tooling.

> Micro-optimizing each line of code as it is written, "because it is fast", without even bothering to use a profiler.

With the CPU, MMU and OS architectures of that period, it wasn't particularly hard to infer what was fast without profiling it. The slow rise in complexity at all 3 levels now makes it hard for even extremely experienced close-to-the-metal programmers to understand what will be fast or slow without a profiler. Times do change, in fact.

Yeah, except the world has moved on from PDP-11, no need to keep asserting something that isn't no longer true.
No, the problem on unix v4 (at least I think that's the version I'm talking about) was that the C compiler did not support passing structs - whether as arguments, return values, or any other expression. So, they didn't do that, because it wouldn't work, and it was less hassle to work around it than fix the compiler. The cargo cult is when people keep avoiding struct passing, even though that compiler deficiency has been fixed for decades now.
That isn't what I was talking about, rather that C only appeared on season 4 of the UNIX movie, when many think it was part of the original cast from the get go.

So it gets a cargo cult status like UNIX was only possible because C was designed to make it happen and other bogus pocus that ignores almost 15 years of previous work in high level languages for systems programming.

> rather that C only appeared on season 4 of the UNIX movie, when many think it was part of the original cast from the get go.

Ah, that makes sense, but that's a different cargo cult than Gibbon1 was complaining about.

Hence why I started my reply to Gibbon1 with "The cargo cult goes beyond that.".
In other languages we create types for just about anything, and like you say, it's strange that we don't do this more in C.
Big problem with C is the standards committee just flat out refuses to add an array type to C. It's deranged because if you had first class arrays it'd be a lot easier to generate code that takes advantage of SIMD instructions.
You mean “add a second array type?”

C has an array type, it’s just a bit wacky, and it doesn't play by the same rules as other types.

Yeah because requiring &array[0] instead of array is so much typing, UNIX V4 would never have been finished.
You want to break existing code? If you made that change, you wouldn’t even be able to pass string constants into functions, because string constants are arrays (and not pointers, as some think).

The C standard committee has a general policy of not breaking code. If you want something like C with a better type system and less of the broken stuff (hello, arithmetic conversions) you can try Zig or one of the others. Once you’re okay with breaking code, you call it a different language, and go in trying to fix lots of things.

Unix V4 predates C standardization anyway.

Who mentioned anything about breaking existing code?

I already decided in 1992 that I don't want to use a broken language, unless when obliged by university work or work requirements.

Unfortunely I have to use software written by people that don't share that opinion and apparently WG14 also has a general policy that improving C safety doesn't matter.

I think zig just blew a big gaping hole in the argument that the language needs to be tied to legacy code.

Because zig can compile old C code and Zig into the same binary. Which means there is no reason you couldn't compile old legacy C code and new better C code into the same binary as well.

The C standards committee need to be fired and replaced with people who aren't willfully holding the language back.

It's not a cult, its just that the cases where the risks of passing by value would be worth any perceived advantage are so few that it just doesn't make sense to even consider it.

It's not like it's a flimsy tribal based claim, the guidance is solid.

It does suck that the usual 64-bit calling convention limits the size of strict passed by value to 64-bits :/.
At least linux AMD64 ABI allows passing (and returning) 128 bit structures by registers, which is perfect for the ptr/len case.
You could store two 32 bit fields and cast the second to a pointer when needed, and the first treat as a length.