42 ms·
Perhaps a stupid questoon: Why isn't a vector type similar to { ptr, count } a normal thing to pass around in C? It's what you reach for in any other language,
by alkonaut 7y ago
Perhaps a stupid questoon: Why isn't a vector type similar to { ptr, count } a normal thing to pass around in C? It's what you reach for in any other language, why did it become idiomatic to pass pointers and lengths separately in C?
A C standard library has a header file for complex math but it doesn't define a simple fixed size array struct? Why is that? Is it because they become pointless when there is no generics to deal with the stride?
- matheusmoreira 7y ago> why did it become idiomatic to pass pointers and lengths separately in C? I've read that it's because there used to be binary interface issues with structures. They can be returned from functions and passed as parameters but it isn't immediately clear how that happens: is it on the stack, in one register or in several registers? Even today there are compiler options that affect the generated code in those cases: -fpcc-struct-return Return “short” struct and union values in memory like longer ones, rather than in registers. -freg-struct-return Return struct and union values in registers when possible. https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-freg-struct-return https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#ind... https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-fpcc-struct-return https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#ind... https://gcc.gnu.org/onlinedocs/gcc/Incompatibilities.html#index-fpcc-struct-return-1 https://gcc.gnu.org/onlinedocs/gcc/Incompatibilities.html#in...
- andrepd 7y ago>They can be returned from functions and passed as parameters but it isn't immediately clear how that happens: is it on the stack, in one register or in several registers? Why does it have to be clear? It can be unspecified and the compiler will do what it thinks is best given the struct, e.g. return `struct {int x,y};` in registers, return `struct int[80] x}` as pointer to memory or write in-place to the caller's stack, via RVO.
- matheusmoreira 7y ago> Why does it have to be clear? Because it's part of the binary interface. Changing the binary interface prevents existing programs from interoperating. Everything breaks until the software is recompiled and that can be a major nuisance at best and impossible in the worst case. Simple, well-defined and stable binary interfaces are a major reason why C is still widely used. Uncertainty in this area is never a good thing so people will actively avoid language features that introduce it. Looks like enums aren't favored for similar reasons: what is the underlying type?
- saagarjha 7y agoBecause then code compiled separately can’t interoperate.
- kps 7y agoOriginally C didn't have the ability to pass or return structures. That was added in the V7 period, after some habits were well established.
- asveikau 7y agoIt doesn't have to be a return value though. You could pass pointers to it as parameters. To answer the question though, a number of people do define structures containing a buffer and a length (and potentially capacity), there just isn't such a structure standardized so everybody who wants to do this has to bring their own. Some examples from Unix: iovec, sendmsg/recvmsg. Surely there are others I'm just not thinking of right now. In the Windows world you have UNICODE_STRING and similar structures. SChannel has "PSecBufferDesc". Again, surely there are others. And prominent libraries might also have their own.
- int_19h 7y agoPassing struct arguments by value is also complicated in some ABIs. The most reliable way is to pass a pointer to struct, but with slices that means double indirection. I don't think ABI is a concern these days, though, even if things were different 30 years ago. The specs that we have today do cover how to pass and return structs in the standard manner.
- glxxyz 7y agoBecause C is a thin layer above assembly language. A pointer fits in one register, { ptr, count } would require two registers. Also if the count is being passed around, it should surely be checked when doing ptr + i, which slows things down further and is unnecessary if the caller knows what they are doing. If you start trying to make C safe and idiot-proof you also make it slower.
- alkonaut 7y agoIsn’t ptr+count always two registers if passed as two parameters too? It seems like just a syntactic difference. A ptr to an array of unknown length can’t be used without the length being passed around next to it whereever it’s passed. You cant deref ptr+i without knowing that it’s under length etc. Two bits of data that belong together seems like they would be convenient to pass (or return!) as one argument or return value. It’s especially terrible with methods that e.g take two lists and return a third. That should be two arguments and a return value, not six arguments.
- pjmlp 7y agoYet plenty of Assembly languages have opcodes for bounded checking memory accesses, some of them in computer systems developed in the early 60's, 10 years before C was born.
- Matthias247 7y agoIt's indeed a weird thing. It should have been an easy addition to the standard library. But instead people either pass around pointer/length pairs all the time - or even worse: They rely on null-terminated strings / arrays. I had discussions with people who claimed that null terminated strings are the only idiomatic thing to do in C - because that is how C does strings. They assumed that since the standard library only provided methods which acted on those kinds of strings it was a preferred way to do things. Even though that is a lot less efficient than the string/array types that other languages use as defaults.
- adrianmonk 7y ago> Why isn't a vector type similar to { ptr, count } a normal thing to pass around in C? For one thing, as I recall in the original K&R version of C (before ANSI C89), the language didn't support passing a struct as a function argument or return value. That means if you did make a struct, then every time you wanted to pass one of these pairs around, you'd have to pass a pointer to the struct, and you'd have to dereference that on every use. Which is arguably just as cumbersome as just passing two arguments, at least in terms of how much code you have to type. Plus it was probably slower. From there it's no surprise if using separate arguments becomes the normal, idiomatic way to do it.
- alkonaut 7y agoThat’s a good reason. And a weird restriction (with 2000’s glasses on designing an ergonomic language).
- int_19h 7y agoGiven the circumstances of C's birth, it was, in many ways, optimized to be ergonomic for the compiler writer. If you don't ever need to return structs, then you can always just use the same register to hold the return value. Similarly, if there are no struct-typed arguments, then each argument is either a register, or a single machine word on the stack (C did other things for the latter - e.g. consider the conversion rules for functions called without being declared).
- pjmlp 7y agoQuite to be expected given its ancestry, BCPL was originally designed only to bootstrap CPL.