6

I was given a (C++) code where arrays are passed using

void fun(int *& name){...}

but what is the idea behind that? I guess it means "an array of references" but when you just pass a pointer to the first element that would be fine, wouldn't it? So what is the motivation to do it this way?

3
  • 3
    It means reference of a pointer variable. The purpose is to change the pointer variable passed to it. Commented Aug 29, 2014 at 7:04
  • 3
    Arrays? It is not possible to pass actual array objects through such parameter. What you can pass is a pointer that points to some array element, but not the array itself. For example, you won't be able to directly pass an int a[10] array to this function. Commented Aug 29, 2014 at 7:13
  • It is important to know that arrays are not pointers, and pointers are not arrays. See this post for more on that. Commented Aug 29, 2014 at 8:33

2 Answers 2

5

The function receives a reference to a pointer. This means that the function can not only modify the int that is pointed to by name, but also that changes to the pointer itself made within the function call will also be visible outside.

Example:

#include <iostream>

int* allocate()
{
    return new int();
}

void destroy(int*& ptr)
{
    delete ptr;
    ptr = NULL;
}

int
main(int argc, char *argv[])
{
    int* foo = allocate();

    std::cout << foo << std::endl;

    destroy(foo);

    std::cout << foo << std::endl;

    return 0;
}

Output is:

0x82dc008
0
Sign up to request clarification or add additional context in comments.

Comments

2

It means that the function can modify the value of the pointer in the caller.

i.e.

myName* foo; /* ToDo - initialise foo in some way*/
fun(foo);
/* foo might now point to something else*/

I regard this as an anti-pattern. The reason being that people reading your code will not expect foo to be modified in such a way since the calling syntax is indistinguishable from the more normal function void anotherFun(int * name){...}.

The stability of such code can suffer. As such, I'd recommend your using void fun(int ** name){...}. The calling syntax then becomes fun(&foo) which indicates to the function user that foo might be modified.

5 Comments

I would say it is an anti-pattern to use a pointer when you can use a reference. But I would do something different too: int* fun(int* name);, and return the new value.
Well, I personally don't like the pointer to pointer syntax where it is not really needed. So it depends on personal preference. However, I agree that in general it might come unexpected to the caller that the pointer can be modified. An alternative to using int** as you propose is to have a clear and well documented API with well chosen names for the functions, e.g. deleteAndSetNull(int*& ptr) for a function that deletes a pointer and sets it to NULL.
It's funny how divisive this is: I obviously respect your opinion and like the proposal to use int* fun(int* name). I dislike references used in the way the OP has as they make code hard to read and bugs can be introduced by juniors working on collaborative projects. At least use a const & if possible.
If you want to ensure that foo wouldn't be modified, you can use fun(myName *const &name)
@PaperBirdMaster: Indeed and good advice indeed if taking a value copy is expensive. (In this case though taking a reference will be slower than passing by value unless the compiler optimises out the internal reference counts).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.