2

Why should I use *& after the type name (T*&) in the following code? Isn't a a pointer to a pointer? If it is, then why should I not use ** instead?

template <class T> void CreateArray(T*& a, int n)
{
   a = new double[n];
}

int main()
{
  double* a;
  CreateArray<double>(a,3);
  return 0;
}
Mo Sanei
  • 445
  • 6
  • 22
  • 3
    It is a *reference* to a pointer – Andy Prowl May 30 '13 at 17:04
  • 4
    No, it's a reference to a pointer. – Oliver Charlesworth May 30 '13 at 17:04
  • Would you agree with Oli? It seems like you're asking how a reference acts differently than a pointer. – Drew Dormann May 30 '13 at 17:10
  • Note that you don't really want to use *any* of the above. `std::vector a(3);` is the right way to accomplish what you're doing above. – Jerry Coffin May 30 '13 at 17:11
  • I want to know the reason why I should add the `&` to the pointer. From what I understand, it enables me to edit the pointer's address (using `new`), am I right? – Mo Sanei May 30 '13 at 18:06
  • @Oli Charlesworth No, that question does not address my problem. I know how pointers and references work but I still have trouble when it comes to passing array pointers to functions. It would be great if someone explained that to me. – Mo Sanei May 30 '13 at 18:14
  • @MohammadSanei: But in that case, which part are you having trouble with? You have a reference to a variable. That variable happens to be a pointer. That's all. – Oliver Charlesworth May 30 '13 at 18:17
  • @Oli Charlesworth It makes me able to edit the pointer's address (using `new`), right? – Mo Sanei May 30 '13 at 18:24
  • @MohammadSanei: You're modifying the pointer, not its address... – Oliver Charlesworth May 30 '13 at 18:31
  • @Oli Charlesworth So why should I make a reference to it if I am modifying the pointer itself? – Mo Sanei May 30 '13 at 18:34
  • @MohammadSanei: Because otherwise you would be modifying a copy... – Oliver Charlesworth May 30 '13 at 18:35
  • @Oli Charlesworth Now I get it! I thought pointers to arrays were different from pointers to other data types because of the fact that the name of an array itself represents a pointer. That was why I thought I needed to use a pointer to a pointer. Thanks for pointing it out! – Mo Sanei May 30 '13 at 18:54

2 Answers2

5

Isn't a a pointer to a pointer?

No, it is a reference to a pointer.

then why should I not use ** instead?

You could do this, but it makes the usage, both on the calling side and within the function, a bit more complicated. However, you could write a function with the same overall effect in either way.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 1
    references are also safer, a pointer is just a variable that stores an address, and that address it's not necessarily a valid, readable, location in memory, it can also be something that you are NOT looking for. – user2384250 May 30 '13 at 17:08
  • 4
    @user2384250: In practice, there's no guarantee that a reference refers to a valid object either... – Oliver Charlesworth May 30 '13 at 17:09
  • 1
    @OliCharlesworth well, we leave the "good practice" to the programmer, didn't we ? – user2384250 May 30 '13 at 17:13
  • @user2384250: It's not (just) a question of "good practice". It's also a question of not accidentally creating an invalid reference, e.g by deleting an object, modifying a container, or dereferencing a null pointer. (Although of course there are "good practices" which can help avoid doing these things). – Mike Seymour May 30 '13 at 17:18
2

T*& is a reference to a pointer, now while references behind the scenes may be implemented as a pointer, it is not the same thing as a T**. You can definitely use a T ** to accomplish the same thing but it would not be idiomatic C++ and it would not be as simple, using references allows you to not worry about how to pass by reference.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • To add to Shafik's excellent summation, while passing a pointer by reference doesn't seem like a big deal, passing a reference to an arbitrary class allows you to use simpler syntax and semantics, and presents a safer, easier, and more maintainable way to use arguments as output parameters. Love references, embrace them, use them instead of pointers when you can. – Ben Brammer May 30 '13 at 17:13
  • How would using `T**` be less idiomatic or less simple (disregarding the few extra characters). I can definetly see a case for `T**` over `T*&` here, since the former makes it explicit to the caller that the function might modify the pointer while you can't differentiate between `T*&` and `T*` parameters when looking at the calling code. Of course the code is horrible either way, but that is really beside the point. – Grizzly May 30 '13 at 17:22