6 ms·
Multiple return values in C at the cost of one weird input parameter
- th3iedkid 9y agoDid not understand what is so special?
- codingbinary 9y agoSame here. I thought that's pretty normal. The generic one looks pretty...normal to me. The clang version iss too much compiler voodoo and kinda makes it less readable.
- userbinator 9y agoI 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.
- redipyni 9y agoCompilers hate him!
- chewxy 9y agoIsn't this how any multiple returns are handled? Return a tuple. Then language supports allow splatting of it, that's all. What am I missing?
- tom_mellior 9y agoYou're missing the fact that it's April 1st somewhere on Earth.
- tehwalrus 9y agoYeah, just return the struct by value. By the way, if you do C programming and haven't, read 21st Century C: http://shop.oreilly.com/product/0636920033677.do http://shop.oreilly.com/product/0636920033677.do
- mar77i 9y agoHow about return out; (void)zero; instead of the whole preprocessor magic for clang? And while we're taking suggestions, use a named struct, that keeps you from making up a new type for every case.
- beeforpork 9y agoSorry to be blunt -- I mean no offense, we all started somewhere. To me, it reads like a beginners experiment. All that #pragma stuff, that first param ('zero') that is all not needed for the effect. The 'zero' and 'return;' stuff reads to me like someone is assuming typeof() evaluates its argument so someone must do somethings against it executing. It looks bloated with stuff a beginner does not realise is unnecessary. I suppose the point is to declare a tuple directly as a return type without a typedef. Then, to return a value, use 'typeof' to declare a return variable of that anonymous type. So, the whole point is to avoid a typedef, I think: typedef struct { int x; int y; } foo_result_t; foo_result_t foo(int a, int b) { return (foo_result_t){ a*a, a/b }; } int main(void) { foo_result_t a = foo(40,2); } Instead of typedef, use typeof and __auto_type. Without the bloat: struct { int x; int y; } foo(int a, int b) { typeof(foo(a,b)) out = { a*a, a/b }; return out; } int main(void) { __auto_type a = foo(40,2); } It is a bit underwhelming. But it tought me something: I did not know GCC's (and Clang's?) __auto_type extension (new since gcc 4.9, apparently). It looks great for compiler specific magic macros! :-)
- yami 9y ago- Step 1 return struct - Step 2 post on HN - Step 3 ??? - Step 4 Enjoy
- imode 9y agohow did this make the front page? it's a demonstration of returning a struct by value... how is this... 1. multiple return values? 2. costing one "weird input parameter"? 3. worthy of a front-page mention?