3 ms·
> 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 ANS
by 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.