1

One thing that I have not been able to understand is when to use certain types of pointers for arguments in functions.

Consider a function that receives an integer as its parameter, and doubles whatever that value may be. A function for that could be:

void doubleTheValue(int *myNum)
{
    *myNum *= 2;
}

int main()
{
    int number = 2;

    doubleTheValue(&number);

    // prints 4
    cout << number << endl;

    return 0;
}

This makes sense to me. The function receives an integer pointer, and you pass in a reference to the variable 'number' and it changes the value. Now, what confuses me is if you did this instead:

void doubleTheValue(int &myNum)
{
    myNum *= 2;
}

int main()
{
    int number = 2;

    doubleTheValue(number);

    // this also prints 4
    cout << number << endl;

    return 0;
}

Note the argument for the function is different. What exactly is this doing internally, and why would you use it over the aforementioned method?

Jessie
  • 2,319
  • 1
  • 17
  • 32

4 Answers4

2

What exactly is this doing internally, and why would you use it over the aforementioned method?

The & reference is to be read as an equivalence for the pointer reference parameter, but

  1. ... with guaranteed initialization
  2. ... without need to use a pointer dereference operator * to access the value/members inside the function implementation
  3. To indicate error conditions for the passed parameter you'll need to use a return value type (not void), or throw an exception from inside your function. This generally applies to both variants.
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 3. ... with no information at the call-site about whether passing this argument to the function will have an effect on the variable's value after the call. – Andrey Mishchenko Dec 16 '13 at 22:25
  • @Andrey because the call side cannot see the function prototype? – juanchopanza Dec 16 '13 at 22:27
  • @juanchopanza sure you can see the function prototype, but if you are scanning through code do you want to search through a bunch of headers to try to find it? (Yes, bla bla I know IDEs make this easier.) – Andrey Mishchenko Dec 16 '13 at 22:29
  • @Andrey well, that is the way it is. You should know the semantics of what you are calling. Passing a pointer doesn't help in this respect. – juanchopanza Dec 16 '13 at 22:30
  • @juanchopanza Andrey is quite right in a sense. The lack of explicit syntax makes the use of references less obvious (at least in function arguments). Passing a pointer does help for exactly this reason. (How about the stupid bastards like me who don't use IDEs?) –  Dec 16 '13 at 22:39
  • 1
    @H2CO3 but once you pass a pointer you have to go and look in the function's *body* to see if it expects a pointer to a single element, or the first element of an array. Or maybe it assumes a pointer to a dynamically allocated object (or array) which it then de-allocates... – juanchopanza Dec 16 '13 at 22:44
  • 1
    @juanchopanza That's not the point, though. If you are using references, you can never tell from the syntax for sure if an object is modified by a function. If, however, only by-value and by-pointer argument passing is allowed, then you can make sure that `foo` is **surely** not modified if it's passed by value, as in `bar(foo)`. So it will be easier to reason about the behavior of the program then (yeah, I've met functional programmers). Of course, if it is passed by pointer (`bar(&foo)`), then it may or may not be modified, but that's then far out of scope. –  Dec 16 '13 at 22:51
2

What exactly is this doing internally, and why would you use it over the aforementioned method?

In your first example:

void doubleTheValue(int *myNum)
{
    *myNum *= 2;
}

you are passing a pointer to the function. A pointer has an implementation defined size. For example, in 64bit architectures the size of a pointer might be 8 byte. In this case (and in general, for primitive types) it's counter productive to pass pointers instead of references (see next paragraph).

In your second example:

void doubleTheValue(int &myNum)
{
    myNum *= 2;
}

you are passing a reference to the function. The main difference is that a reference is not even required to take any memory at all. myNum and the original variable could just share the same object in memory.

For other differences between references and pointers I suggest you to take a look at this question. But a general rule of thumb it to always use references when you can. You'll find the need of using pointers in specific situations (like, for example, when you want to accept a null-value).

Community
  • 1
  • 1
Shoe
  • 74,840
  • 36
  • 166
  • 272
1

You should understand the difference between a pointer and reference. In many cases we cannot do something using references which we can do using pointers. Although references do not need any memory and they are just tags to a memory location but in comparison with pointer, pointers are far more powerful than references. The major differences are:

1- A pointer can be re-assigned any number of times while a reference can not be reassigned after initialization.

2-A pointer can point to NULL while reference can never point to NULL.

3-You can't take the address of a reference like you can with pointers

4-There's no "reference arithmetic" (but you can take the address of an object pointed by a reference and do pointer arithmetic on it as in &obj + 5)

Ali Raza
  • 191
  • 2
  • 12
0

From OP, in comments of original post:

Neither of this really give any use for applications nor explain why you would use one over the other which is what I am curious about.

My impression is that people mostly use references because they make the code look cleaner, so that there aren't a lot of * and & floating around.

Andrey Mishchenko
  • 3,986
  • 2
  • 19
  • 37
  • haha @ downvote. Look around in those other posts on the topic, and in the C++ faq at cplusplus.com. They say to prefer references over pointers, but nowhere is any reason actually given, and the StackOverflow responses advocating this practice cite that FAQ, which ALSO doesn't give any reason! – Andrey Mishchenko Dec 16 '13 at 22:38
  • 2
    that's not quite right, and I don't think your answer gets the point either. C++ people usually prefer references because they argue that pointers are dangerous. –  Dec 16 '13 at 22:41
  • While you're right that the "pointers are dangerous" argument is the one that stands up best to serious scrutiny, I think it's still probably true that the *reason* that references are so prevalent in code is that the syntax is prettier, even when it is less informative. – Andrey Mishchenko Dec 16 '13 at 22:45