3 ms·
I guessed the "one weird input parameter" would simply be the pointer to the structure to "return" multiple values, but it turns out I was wrong --- it is using
by userbinator 9y ago
I guessed the "one weird input parameter" would simply be the pointer to the structure to "return" multiple values, but it turns out I was wrong --- it is using the syntax of returning a structure by value, which depending upon the exact compiler, could be implemented in much the same way as an out-pointer parameter. i.e.
struct Foo foo = return_foo();
becomes
struct Foo foo;
return_foo(&foo);
That said, the ability to pass, return, and assign structures by value seems to be one of the lesser-known features of C, so this serves as a good example.
- haiihaii 9y agoI've gotten quite used to doing just this. It started out as a habit from cpp where I relied on RVO, but for small structs the price of copying it vs copying a pointer to it seems extremely minimal apart from the temporary double allocation. Also, does anyone know if gcc or clang does something along the lines of RVO behind the scenes? I fail to see any side effects from it, so it should be entirely possible, no?
- mbel 9y agoIt really depends on the target architecture (and ABI), size of the struct and how fields in the struct are defined, more than if it's C or C++. For simple examples like this one: https://godbolt.org/g/ZT3bJ3 https://godbolt.org/g/ZT3bJ3 there usually will be no problem.