4 ms·
> 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 empt
by layoutIfNeeded 7y ago
> 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.
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-convention?view=vs-2019#return-values https://docs.microsoft.com/en-us/cpp/build/x64-calling-conve...
- jacquesm 7y agoYes, but that's literally an implementation detail and this could be done in many different ways. The idea here is to gain some amount of expressiveness, if that is the goal then implementation details should be of lesser importance. Moving structures around 'by value' rather than 'by reference' is wasteful in my view, but if you have to then you should no longer worry about the underlying mechanism until optimization time rolls around.