by miohtama · 6y ago · 0 comments
So either return the whole struct as values or, alternatively - and this is the way most library functions do it - call the function with a pointer to an empty struct which the function then fills. That leaves the return value for error status.
Mind you, under the hood all your structs will be returned through pointers to caller-allocated memory, unless it can fit into 8 bytes.
“To return a user-defined type by value in RAX, it must have a length of 1, 2, 4, 8, 16, 32, or 64 bits. [...] Otherwise, the caller assumes the responsibility of allocating memory and passing a pointer for the return value as the first argument.”
https://docs.microsoft.com/en-us/cpp/build/x64-calling-conve...
I'm not sure what you mean by this. Tuples are typically isomorphic to structs with unnamed fields.
let res, _, _ = fun_which_returns_3_tuple ()
vs struct my_result { int res; int other; int third; };
...
struct my_result r = fun_which_returns_struct ();
printf ("%d\n", r.res);It’s really rather common eg several time or socket functions take a pointer as « output parameter », fill it and return an error code.
Not the best but not the worst either.
C has pointers which can easily enough be used as "out parameteters". In fact the C standard library regularly makes use of these. It's not amazing, but it works fine, and it's usually fairly easy to go back and forth e.g. expressing a stat(2) call is reasonably straightforward.
The C API could do this, it just doesn’t.
> More advanced languages should not be forced to deal with it
(I'm more in agreement with the definition you offer than your usage.)