Sign in to follow this  
jagguy

pointer to pointer

Recommended Posts

jagguy    100
i diont get the point (so to speak) of passing a pointer to class to a function as a pointer pointer to class or like fname(Class** myclass.. why could just passing the pointer to class do as that would have saved space and be able to change values?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
The function can then obvously replace the pointer to another pointer to an object of the same class if needed. Of course, that could be done with a reference to a pointer too...

Share this post


Link to post
Share on other sites
Hinch    244
A pointer-to-a-pointer-to-an-object won't be any larger than a plain pointer-to-an-object - in terms of memory usage it's still just a pointer, it's just that it points to another pointer instead of directly to an object. A pointer-to-a-pointer is useful because (for example) the object pointed to by the "plain" pointer can be modified as usual, but also can be deleted, new'ed, or generally moved around in memory, and the pointer to that object can then be modified to point to the new location in memory. So it not only allows you to modify the object pointed to, but also to modify the pointer itself to make it point somewhere else. Ehm, not sure that explanation is 100% clear but I hope that helps a bit. :-)

Share this post


Link to post
Share on other sites
Nemesis2k2    1045
When you see an argument that is a pointer to a pointer, what it probably means is that variable is a pointer to an array of pointers. A pointer to a pointer by itself is pretty useless.

Share this post


Link to post
Share on other sites
MaulingMonkey    1730
Quote:
Original post by Nemesis2k2
A pointer to a pointer by itself is pretty useless.


I disagree. In pure C, references being unavailable, to allow a argument to be modified one of course needs to put in an extra level of indirection. So, if the C++ style argument would be a reference to a pointer, the pure C style version would be a double pointer.

This results in at least a good many APIs which target C (possibly along with other platforms) using this methodology. This might seem irrelevant since the question appears to be targeted for C++ but it's not for the following reasons:

1) Consistancy. It dosn't make sense to name one variable SomeRandomVar and another some_other_var. The same goes for swapping between references and pointers to simply allow the "effective" argument to be modified.

2) Portability. Prehaps a bad term - what I'm getting at is APIs designed to work with C and C++ will need to use pointers instead of references since C does not provide references.

I've seen more than one manpage with double+ pointer functions outlined in them. If they're pretty much useless, they wouldn't get used much would they?

Share this post


Link to post
Share on other sites
petewood    819
A pointer to an integer means you can change the value of the integer so that the calling code gets the new value.

So, a pointer to a pointer (to something) means you can change the value of the pointer so that the calling code gets the new value.

Anyway, do people still use pointers these days? (c:

Share this post


Link to post
Share on other sites
Sharlin    864
In C, consider especially:


void swap(int **a, int **b)
{
int *tmp = *a;
*a = *b;
*b = tmp;
}

void foo(void)
{
int big_buffer1 = malloc(10000, sizeof(int));
int big_buffer2 = malloc(10000, sizeof(int));

/* do stuff */

swap(&big_buffer1, &big_buffer2);

/* do more stuff */
}



Mucha faster than swapping the actual contents of those buffers.
But, as has been said, in C++ one would typically use references to
pointers, not pointers to pointers.

Share this post


Link to post
Share on other sites
Nemesis2k2    1045
Very well. I haven't worked with pure C, so I don't think in terms of what makes sence for it. In C++, a seperate function being responsible for reallocating a block of memory it doesn't own is a big nono.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this