0

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

I see there are (many/all?) cases where 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: The question cited (about general differences pointers vs. references) as containing the answer to this OP is very much related, but I doubt it is a dupe. The differences between pointers and references, as for passing to functions, can surely be inferred from the answers there, but they are also probably much more specific than the general differences asked about there. So not all of the answers there may be relevant here. Moreover, some answers there may be only partially relevant here.

  • 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

1

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
1

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
  • 621
  • 6
  • 14