4 ms·
There are performance reasons why you may not want to do so, though. GCC has a warning option (-Waggregate-return) to detect this.
by apaprocki 12y ago
There are performance reasons why you may not want to do so, though. GCC has a warning option (-Waggregate-return) to detect this.
- DSMan195276 12y agoI'm curious on the performance issues. IIRC, common x86 C calling convention does returning structs by requiring the caller to allocate memory for the struct (on the stack or otherwise) and then pass a pointer. If that happens then it becomes no different then if you had simply allocated the array on the stack and then passed a pointer to it to the function. Are the performance issues in the case the architecture does aggregate type returning differently?
- fit2rule 12y ago>I'm curious on the performance issues. Whether you can put your return value in a register, or whether you need to housekeep the stack. That's a performance difference.
- apaprocki 12y agoIf you know your target and know your compiler very well (and make sure that nothing changes the assumptions made over time) then I suppose you can take advantage of that. C++ also has return value optimization but there is no way for the author of the code to know if the various platforms/compilers will actually optimize it, so blindly relying on it can result in poor performance. And yes, different platforms handle it differently. IIRC SysV PowerPC only supports up to 64 bits in r3,r4 but AIX POWER forces all returned structures into memory. I think SPARC ABI prior to V9 did not use registers either.
- tpush 12y agoNote that, according to SysV AMD64 calling convention, structs with integer/pointer members up to 128 bits(including padding) are passed in registers. ARM 64 bit is similar in this respect, I believe.