views:

1651

answers:

6

I'm kind of new to C++ and have some questions, this is one of them.

Is there ANY reason when you are using a function that takes in one or several parameters, parameters of which you know will always be stored in a variable before the function call, to pass a copy of the variable, rather than a pointer to the variable?

I'm talking in terms of performance. Seems to me that it would take a lot more resources to pass a copy of a whole struct than just a pointer(4 bytes).

+1  A: 

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

tloach
+3  A: 

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

unwind
I would add that non-0 null pointers are even more annoying.
tloach
What are non-0 null pointers?
Motti
int * ohoh = new int; delete ohoh;cout << ohoh; // non-0 null pointer
MattyT
otherwise known as wild pointers
this is why i'm not testing for null pointers (unless it stated that function can return null). Null pointer is very small subset of global "invalid pointer" problem. Why don't we check for example for 1 will be clearly invalid as 0 (with few exceptions where 0 is valid as well) ...
Ilya
+2  A: 

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

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

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

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

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

Ned Batchelder
+3  A: 

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

yesraaj
+5  A: 

There are several ways in which passing 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 are other reasons for wanting to pass a copy rather than a reference, namely your function will be making changes in the structure which are not to be reflected back to the caller. While this is generally a poor practice, making the parameter pass-by-value will ensure that the caller's view isn't changed incorrectly.

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

+4  A: 

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

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

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

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

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

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

Motti