0

When passing vectors to functions, which method should be used: reference or pointer?

In most cases any of the two would do the job. Even in those cases, are there any differences in the way the two methods perform?

I do not see if there are cases where only one of the two would work. Is there any such case?

EDIT: I will call Q1 this question, Q2 the question cited as containing the answer to this OP, and A1/A2 the respective sets of answers. Q2 asks about general differences pointers vs. references (not for passing to functions). That is very different from this Q1 because:

  1. Many answers for Q2 may not be relevant for Q1.
  2. Whatever contents in answers for Q2 that is relevant for Q1 would have to be sifted by the readers, some of which may be even unable to do that.

So tagging this a dupe is probably more detrimental for than helping the readers.

  • 1
    Also see [When to pass by reference and when to pass by pointer in C++?](https://stackoverflow.com/q/3613065/608639), [Pass vectors by pointer and reference in c++](https://stackoverflow.com/q/26218356/608639), [Vector pointers and passing objects by reference in C++](https://stackoverflow.com/q/27281966/608639), [What are the differences between a pointer variable and a reference variable in C++?](https://stackoverflow.com/q/57483/608639), [Difference between references and pointers](https://stackoverflow.com/q/745878/608639), etc. – jww Sep 22 '18 at 10:10

2 Answers2

3

This is mainly preference. I follow the following rule: Use (const) references when you can and (const) pointers when you have to.

A reference implicitly guarantees not pointing to null. Violating this guarantee leads to undefined bahvior, so make sure you check if you convert a pointer to a reference! In my opinion this is the only main advantage.

I do not see if there are cases where only one of the two would work. Is there any such case?

If the vector is on the heap and you only hold a pointer to it, than it may be a nullptr. Than you cannot pass a reference, because a reference may not be null. If you fail to do so it invokes undefined behavior.

  • Reference also allows to capture temporals. You can't do a pointer to temporal! – Swift - Friday Pie Sep 10 '18 at 12:51
  • @Swift-FridayPie True, but I am unsure how this can be used to pass an argument. –  Sep 10 '18 at 13:11
  • @Swift-FridayPie Well, I capture a reference and pass this one on to a function. But this passing can again be by reference or by ptr. –  Sep 10 '18 at 13:22
2

Most people would agree that reference is to be preferred over raw pointers whenever you have the choice.

Are there reasons for preferring one or the other here?

There is one big difference between pointers and references: References cannot be null.

If you write a function that takes a pointer, you must add a nullptr check somewhere. With references that is not required and sames some typing.

In addition to that, pointers can introduce errors that cannot happen when using references (mostly due to pointer arithmetic) and are generally consider to not be as safe as references.


Personally, I use references for input parameters but whenever a function returns something in a parameter I'd rather use a pointer to make the intent clear on the call site.

void foo(int* var) {
   /*
    there may be a lot of code here
   */

   *var = 0;
}

// somewhere else
foo(&var);

Here it is clear that var can be modified by the foo function, while the same is not as clear when using references:

foo(var)

The latter requires you to inspect the function definition to see whether it modifies var or not.

markhc
  • 659
  • 6
  • 15