简体   繁体   中英

Function parameters: Copy or pointer?

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).

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!

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.

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.

避免使用pointer.Use常量引用if是IN参数否则只是IN OUT参数的引用

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.

引用更常见,如果它们不会改变,则引用const引用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM