[ACCEPTED]-Function parameters: Copy or pointer?-pointers

Accepted answer
Score: 12

There are several ways in which passing 16 a copy can be cheaper than passing a pointer.

  1. The object is equal to or smaller than a pointer. Directly accessing a value will always be faster than dereferencing a pointer.
  2. The structure is small enough to be put on the stack by the compiler. In this case, access to the values in the structure is done by indexed addressing modes rather than indirect, indexed addressing modes. The former are generally faster.

There 15 are other reasons for wanting to pass a 14 copy rather than a reference, namely your 13 function will be making changes in the structure 12 which are not to be reflected back to the 11 caller. While this is generally a poor practice, making 10 the parameter pass-by-value will ensure 9 that the caller's view isn't changed incorrectly.

Now 8 for the part of the answer you probably 7 don't want to hear: It generally doesn't 6 make that much difference! Use the parameter 5 passing method that makes the most sense 4 for the semantics of your program. If you 3 find later that there is a performance bottleneck 2 in a particular area then focus in on improving 1 the performance there. Don't over optimize!

Score: 10

Passing an object by pointer (or reference) and 24 passing a copy of the same object have different 23 semantics. If you want changes you make 22 on an object to be reflected outside the 21 function call you want reference semantics, otherwise 20 you want value semantics.

Commonly value 19 semantics can be expressed either by passing 18 the value by value or by const reference

void value_semantics(my_obj obj);
void value_semantics(const my_obj& obj);

However 17 the const reference way has some drawbacks 16 as well, it prevents several optimizations 15 that the compiler may make because of aliasing issues also 14 for objects with trivial constructors the 13 extra level of indirection (a reference 12 is implemented as a pointer) may outweigh 11 the benefits of avoiding the copy.

In order 10 to get reference semantics you can choose 9 either passing by reference or by pointer, as 8 others already said references are more 7 natural than pointers in C++ (you don't 6 have to use the address of operator &) and 5 the only real advantage of pointers is if 4 you want to enable NULL values.

The rule 3 of thumb is that for non-trivial classes 2 you should pass by const reference, otherwise 1 by value.

Score: 4

A pointer opens up for bugs, since it allows 6 the callee to alter the object. Pointers 5 can be 0, which tends to create crashes, and 4 this creates a need to test for 0 pointers 3 all over the place, this can be annoying. Using 2 C++ references, const-declared where possible, circumvents 1 both these problems.

Score: 4

Avoid using pointer.Use constant Reference 2 if is IN parameter else just reference for 1 IN OUT parameter

Score: 2

The main question isn't about performance, but 23 semantics, and whether your function modifies 22 the data in the structure.

If your function 21 modifies the structure, then passing a pointer 20 will let the caller see the changed data 19 in the structure. In this case, passing 18 a copy will likely be wrong, since your 17 function will modify a copy which is then 16 (presumably) discarded. Of course, it is 15 possible that your function modifies the 14 data, but you don't want the modifications, in 13 which case a copy is the right thing to 12 do, to protect the original values from 11 changes.

If your function doesn't modify 10 the structure, then there's no reason to 9 copy the values, since they will only be 8 read.

If you are not comfortable with the 7 concept of passing pointers to structures, you 6 should get some practice, because it is 5 the typical way of dealing with structures 4 in C and C++.

As far as performance goes, it's 3 more work to copy the structure, but it's 2 fairly minor in the scheme of things. Keep 1 your mind on the semantics of the code first.

Score: 1

a reference is more common, or a const reference 1 if they aren't going to change.

More Related questions