On 2016-02-26 4:23 AM, Rob Stewart wrote:
On February 26, 2016 12:49:19 AM EST, Phil Bouchard
wrote: On 2016-02-25 8:38 PM, Rob Stewart wrote:
On February 24, 2016 10:22:03 PM EST, Phil Bouchard
wrote: 2) I would pass the pointer parameters as references
That would be a pessimization for the 99% case, I would imagine.
I'm pretty sure these references will help out optimization of the raw pointer parameters as well.
Since references are implemented as pointers, how would pointer to pointer arguments help anything?
1) Let's assume we have this program: #include <iostream> using namespace std; inline void foo1(int * p) { *p = -1; } inline void foo2(int * p) { foo1(p); } inline void foo3(int * p) { foo2(p); } inline void bar1(int * & p) { *p = -2; } inline void bar2(int * & p) { bar1(p); } inline void bar3(int * & p) { bar2(p); } void __attribute__ ((noinline)) foo(int * p) // by value { foo3(p); } void __attribute__ ((noinline)) bar(int * & p) // by reference { bar3(p); } int main() { int * p = new int; cout << __FUNCTION__ << ": " << *p << std::endl; foo(p); cout << __FUNCTION__ << ": " << *p << std::endl; bar(p); cout << __FUNCTION__ << ": " << *p << std::endl; } 2) It outputs the following: main: 0 main: -1 main: -2 3) And when I look at the dissasembly: 00000001004010d0 <_Z3fooPi>: 1004010d0: c7 01 ff ff ff ff movl $0xffffffff,(%rcx) 1004010d6: c3 retq 00000001004010e0 <_Z3barRPi>: 1004010e0: 48 8b 01 mov (%rcx),%rax 1004010e3: c7 00 fe ff ff ff movl $0xfffffffe,(%rax) 1004010e9: c3 retq I conclude that there is not much difference between foo() and bar(). But in the case of passing by value, these pointer are just filling up the registers. But what happens when you have more pointers than registers available? The ending assembly will be worse it this case for sure. -Phil