f



pointer to pointer

I have only used pointer to a pointer while manipulating arrays of
arrays. I have not seen any C++ libraries that use pointer to a
pointer as a function parameter. But I have seen it used in COM. If we
want the function to create a new object and allow the passed in
pointer to point to the object created we can pass a pointer to the
reference. Is there anything special about COM that it needs pointer
to a pointer. Is this becuase that COM can be used from C as well and
we don't have references in C.

In what other circumstances pointer to pointer can be used. Why don't
we never see pointer to a pointer to a pointer. Does it have any use
at all?


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
oopsbabies (21)
2/26/2007 1:21:07 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

4 Replies
829 Views

Similar Articles

[PageSpeed] 25

Mitesh wrote:
> I have only used pointer to a pointer while manipulating arrays of
> arrays. I have not seen any C++ libraries that use pointer to a
> pointer as a function parameter. But I have seen it used in COM. If we
> want the function to create a new object and allow the passed in
> pointer to point to the object created we can pass a pointer to the
> reference.

The difference between pointers and references should play a rather
obvious role here...

> Is there anything special about COM that it needs pointer
> to a pointer. Is this becuase that COM can be used from C as well and
> we don't have references in C.
>
> In what other circumstances pointer to pointer can be used. Why don't
> we never see pointer to a pointer to a pointer. Does it have any use
> at all?

The most common occurrance: an argument that is a pointer that needs
to be filled in by the function, but only if the caller asks to:

     int allocate(SomeClass ** ptr)
     {
         if (ptr) { // the pointer is not NULL
             if (someCondition) {
                 *ptr = new SomeClass;
                 return 1;
             }
             else {
                 *ptr = new SomeClass[42];
                 return 42;
             }
         }
         return 0;
     }

Here, the caller can provide the pointer to "fill in"

     SomeClass *ptr(0);
     int howmany = allocate(&ptr);
     .. // use ptr
     if (howmany > 1)
         delete[] ptr;
     else
         delete ptr;

or call it with NULL

     ...
     allocate(NULL);  // passing NULL - nothing gets allocated

(I am sure naming is rather bad here, but you get the idea).

V
-- 
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Victor
2/26/2007 5:24:01 PM
Mitesh <oopsbabies@hotmail.com> wrote:

> I have only used pointer to a pointer while manipulating arrays of
> arrays. I have not seen any C++ libraries that use pointer to a
> pointer as a function parameter. But I have seen it used in COM. If we
> want the function to create a new object and allow the passed in
> pointer to point to the object created we can pass a pointer to the
> reference. Is there anything special about COM that it needs pointer
> to a pointer. Is this becuase that COM can be used from C as well and
> we don't have references in C.
>
> In what other circumstances pointer to pointer can be used. Why don't
> we never see pointer to a pointer to a pointer. Does it have any use
> at all?
   A pointer to a pointer might be used when ever another level of
indirection simplifies the problem.
   remember also
   double *a = ...;
   double *b = ...;
   tyoedef double *Ptr;
   Ptr & c = a;
   Ptr * d = a;
   c = b  // modifies a (a = b)
   d = b  // does not modify a



--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
cbarron3
2/26/2007 9:19:40 PM
On Feb 26, 7:21 pm, "Mitesh" <oopsbab...@hotmail.com> wrote:
> I have only used pointer to a pointer while manipulating arrays of
> arrays. I have not seen any C++ libraries that use pointer to a
> pointer as a function parameter. But I have seen it used in COM. If we
> want the function to create a new object and allow the passed in
> pointer to point to the object created we can pass a pointer to the
> reference. Is there anything special about COM that it needs pointer
> to a pointer. Is this becuase that COM can be used from C as well and
> we don't have references in C.

That's part of it.  (You can often use reference to pointer where C
would use pointer to pointer).

> In what other circumstances pointer to pointer can be used. Why don't
> we never see pointer to a pointer to a pointer.

Because it will (almost) certainly cause the brain of a maintenance
programmer to explode.

I did once handle some code (written in a very C'ish style) which used
double ****p as an argument to an allocation function (it might have
been five stars).

It was supposed to allocate (which implied the first star) an array
(hence the second star) of two-D tables (implemented as a pointer to
an array of pointers to double).

I replaced it with an allocation function which took a reference to
vector<TwoDTable> (along with changing quite a bit of the other
code).  It made an awful lot of code a whole HEAP easier to read!


> Does it have any use at all?
>


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Martin
2/27/2007 8:22:49 AM
On Feb 26, 10:19 pm, cbarr...@ix.netcom.com (Carl Barron) wrote:
> Mitesh <oopsbab...@hotmail.com> wrote:
>    A pointer to a pointer might be used when ever another level of
> indirection simplifies the problem.
>    remember also
>    double *a = ...;
>    double *b = ...;
>    tyoedef double *Ptr;

you mean 'typedef' :)

>    Ptr & c = a;
>    Ptr * d = a;

You left off an ampersand
Ptr * d = &a;

>    c = b  // modifies a (a = b)
>    d = b  // does not modify a

Nor does it even compile.  'd' is a pointer to a pointer.  'b' is a
pointer.  The later can not be assigned to the former.


Pointer-to-pointer is just as useful as reference-to-pointer, except
pointer-to-pointer can be null, while reference-to-pointer can not be
null.

As others have pointed out, a common use case is an object factory:

void create(T** pp) {
  if (pp) *pp = new T();
}
void create(T*& rp) {
  rp = new T();
}

joshua lehrer
http://www.lehrerfamily.com/


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Joshua
3/2/2007 1:14:40 PM
Reply: