Pointer to pointer Vs References to Pointer

B

bansalvikrant

Q . When to use use pointer-to-pointer and reference-to-pointer to
modify a pointer passed to a function ?

According to me

Which is better : double pointers OR Reference to Pointer ?
There is no clear advantage and depends on programmer ‘s choice or
habit . As Nilesh mentioned ” Reference to pointer” makes code easy
But Pointer to pointer makes code easy to understand. So one can use
any one .

Why Pointer to Pointer is generally used ?
1. Pointer to pointer can be used in C and C++ but reference to
pointer is limited to C++.

a. Generally people start learning programming from C . So they
get used to pointer to pointer.
b.Pointer to Pointer usage makes project porting in
C or C++ easy.

2. Generally libraries uses pointer to pointer approach like COM uses
pointer to pointer for CoCreateInstance or QueryInterface.


Fourm : Could you please let me know if these are the only reasons or
i am missing something?

Thanks
Vikrant

Example

#include "stdafx.h"

void Ref2Poi(int * &p)
{
*p = 10;
}

void Poi2Poi( int ** p)
{
**p = 10;
}

int _tmain(int argc, _TCHAR* argv[])
{
int i = 13;
int j = 14;

int *p = &i;
int *q = &j;

Ref2Poi(p);
Poi2Poi(&q);

return 0;
}
 
P

Pascal J. Bourguignon

Q . When to use use pointer-to-pointer and reference-to-pointer to
modify a pointer passed to a function ?

According to me

Which is better : double pointers OR Reference to Pointer ?
There is no clear advantage and depends on programmer ‘s choice or
habit . As Nilesh mentioned †Reference to pointer†makes code easy
But Pointer to pointer makes code easy to understand. So one can use
any one .

Why Pointer to Pointer is generally used ?
1. Pointer to pointer can be used in C and C++ but reference to
pointer is limited to C++.

a. Generally people start learning programming from C . So they
get used to pointer to pointer.
b.Pointer to Pointer usage makes project porting in
C or C++ easy.

2. Generally libraries uses pointer to pointer approach like COM uses
pointer to pointer for CoCreateInstance or QueryInterface.


Fourm : Could you please let me know if these are the only reasons or
i am missing something?

The main difference between them, is at the call site:

Output parameter Input parameter

- with a pointer:

xyz(&p); uvw(p);

- with a reference:

xyz(p); uvw(p);


We can see that with reference, we don't see whether the argument will
be modified or not. On the other hand, with pointers, we have an
explicit notation & to indicate that the argument might be modified by
the function.


For this reason, some prefer to use pointers for output parameters.

I've been formed early with pascal, so I don't mind references, and
lately with lisp, so I don't mind output parameters: use a more
functional style, do not write procedures with side effects, and use
multiple return values if need be. That is, write:

- with functional style:

p=xyz(p); uwv(p);




(Also, don't use pointers, but smart pointers, and don't name the
template SmartPtr, or Pointer, but Object:

Object<Person> p=new Person("Joe","Doe");
p->increaseSalary(2000);
Object<Person> surgeon=p->findGoodSurgeon();
p=surgeon->operate(p);

).
 
J

JC2

Q . When to use use pointer-to-pointer and reference-to-pointer to
modify a pointer passed to a function ?

If you structure your code so that the assignment doesn't happen until
the function is about to return, then there's not much difference in
readability between the two, e.g.:


void PtrToPtr(int **ppint) {
int *pint = new int[50];
pint[0] = 1234;
// etc etc
*ppint = pint;
}

void RefToPtr (int *&ppint) {
int *pint = new int[50];
pint[0] = 1234;
// etc etc
ppint = pint;
}


The only difference is the parameter type and the final assignment.
Also, structuring your code this way means that if your function
returns/throws early because of an error, you can leave the input
value unmodified, which is sometimes desirable. It can also make
cleanup and exception-safety very easy, consider:

void PtrToPtr (Object **pobj) {
std::auto_ptr<Object> obj(new Object);
// do some stuff that may throw, then:
*pobj = obj.release();
}

void RefToPtr (Object *&pobj) {
... same code, then ...
pobj = obj.release();
}

The unwanted Object is automatically freed on early return/throw,
until it is actually assigned to the output parameter.

Then, which way you do it is just a merely of personal style
preference.

That said, I sort of agree with Pascal's reply in that using a
reference to pointer might obscure the fact that it's an output
parameter at the call site. On the other hand, a simple check of the
function declaration would show that it accepts a non-const reference,
and I any non-const reference parameter passed to a function should
generally be assumed to be a candidate for modification, IMHO.

Why Pointer to Pointer is generally used ?
1.      Pointer to pointer can be used in C and C++  but reference to
pointer is limited to C++.

I don't think this is a good line of reasoning without context. It's
true that references can only be used in C++ but that is only a
disadvantage if your code is also meant to be compiled by a C compiler
-- but in that case, you're probably coding in C, not C++. Just use
whatever features the language you are actually coding in supports. If
you are coding in C++ then there's no reason to stick to C because,
hey, you *are* writing C++ after all, not C.

           a. Generally people start learning programming from C . So they
get used to pointer to pointer.

Incidentally, I taught myself C using a C++ compiler, and it really
made a mess of me! :) But again, I also don't think this is a very
compelling reason. Whether or not you are used to doing something some
way in C should have no bearing on the way you do it in C++, as they
are entirely different languages, even though they happen to share a
lot in common.

                   b.Pointer to Pointer usage makes project porting in
C or C++ easy.

If you are porting from C to C++ then, of course, you wouldn't be
using references anyways.

If you are porting from C++ to C then you really have to ask yourself
if you are doing the right thing. I can't think of any situations
where you'd have to do that off the top of my head... maybe if you're
porting to a platform that doesn't have a C++ compiler available? But
what kind of application would be written in C++ then, surprise,
ported to a platform without a C++ compiler?

2.      Generally libraries uses  pointer to pointer approach like COM uses
pointer to pointer for CoCreateInstance or QueryInterface.

This is not true. It is more accurate to say that C libraries use
pointers to pointers. Also, about COM... COM itself doesn't specify
references or pointers or anything like that. COM has bindings in many
languages, e.g. VisualBASIC (where pointers vs. references don't apply
at all). You're probably used to seeing the C bindings. Windows API
functions like CoCreateInstance do not use references because they are
designed to be used by both C and C++ applications. Also COM doesn't
make any statements about class hierarchy relations, hence all the
casts to (void **) in QueryInterface et. al, you can't handle that
with only references.

You may want to have a read here: http://en.wikipedia.org/wiki/Component_Object_Model

Another quality of references is the inability to represent NULL as a
value.

Fourm : Could you please let me know if these are the only reasons or
i am missing something?

None of those are reasons, I don't think. In the implementations of
the functions it does not make much of a difference. When the
functions are called, sometimes pointers-to-pointers are clearer,
sometimes maybe not, personally I look at const vs. non-const rather
than the presence of an "&". Reasons related to C are not generally
sound as C is a different language. I think a lot of it is style.

I also think that consistency is the most important thing. Pick one or
the other. If you have a strange mix of both pointers-to-pointers and
references-to-pointers serving the same purpose, *then* readability
becomes an issue.


J

Thanks
Vikrant

Example

#include "stdafx.h"

void Ref2Poi(int * &p)
{
      *p = 10;

}

void Poi2Poi( int ** p)
{
     **p = 10;

}

int _tmain(int argc, _TCHAR* argv[])
{
      int i = 13;
      int j = 14;

       int *p = &i;
      int *q = &j;

      Ref2Poi(p);
      Poi2Poi(&q);

      return 0;

}
 
J

JC2

Then, which way you do it is just a merely of personal style
preference.

I'm sorry, it appears I was drunk when I typed that. In the last 60
seconds I've sobered up considerably. It was supposed to say something
like:

"Then, which way you do it is merely a matter of personal preference."
 
F

Fred Zwarts

Q . When to use use pointer-to-pointer and reference-to-pointer to
modify a pointer passed to a function ?
Which is better : double pointers OR Reference to Pointer ?
There is no clear advantage and depends on programmer ‘s choice or
habit . As Nilesh mentioned ” Reference to pointer” makes code easy
But Pointer to pointer makes code easy to understand. So one can use
any one

Is a pointer much different from e.g. int in this choice?
The disadvantage when using pointer to pointer is that a check is needed for a NULL pointer.
When using references, the compiler performs this check already.
So, I use pointer to pointer in cases where it is useful to call the function with a NULL pointer,
if the function should never be called with a NULL pointer, I use references.
I find references easier to understand than pointer to pointers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top