当C函数必须返回多个值时,有几种方法可以做到这一点.
Right now I'm interested in the relative efficiency of two of those methods:
a) bundle the values in a struct foo. Populate a local foo, and return that.
b) pass pointers to be populated.
(我正在编写一些混合了两者的遗留代码.)
就本帖子而言:
- 所有返回值都是基元.Int‘s、指针值等.因此sizeof(Foo)非常小.
- 让struct Foo变得不透明并不是一个问题.
- 有问题的函数最多有12个参数,包括任何从PTR到返回值的参数.
- 假设有一点现代的编译器,例如GCC 11或更高版本.
Obviously inlining would make the question moot.
Can the different methods affect the compiler's ability to inline?
If not inlined, will there be a performance difference between the two methods?
在函数参数中放置指向返回值的指针参数会有影响吗?是基于编译器的内联能力,还是基于非内联性能?
为清楚起见,编辑(A).