By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,573 Members | 1,208 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,573 IT Pros & Developers. It's quick & easy.

Usefulness of multiple levels of indirection.

Expert 5K+
P: 6,050
I'm having a hard time grasping the concept of multiple levels of indirection, that is for example a char **. I understand that passing a pointer to some data to a function means that memory for that data doesn't have to be allocated on the stack, thereby reducing overhead (of course memory for the pointer has to be allocated to the stack, 'though). Another use as I understand (and see the benefits for) is that if a pointer to data is given to a function, that function does not have to return new, modified data - this is in contrast to PHP in which you generally do not pass arguments by reference but work with the return-value of the function.

However, my grasp ends around there.

What I'm having trouble understanding is why a function would want, say, struct addrinfo **res parameter. Surely for this function to work with res it will have to dereference it twice (to get to the struct pointed to). Whereas if it was just struct addrinfo *res it would only need one dereference, again getting to the struct.

I use the struct addrinfo **res because I am currently looking at networking with C. The function that brought me to ask this question is getaddrinfo() which has the signature:

Expand|Select|Wrap|Line Numbers
  1. int getaddrinfo(const char *node,
  2.                 const char *service, 
  3.                 const struct addrinfo *hints,
  4.                 struct addrinfo **res);
Thanks for your help,
Dec 3 '09 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 8,916
The thing is the the actual structure of addrinfo is somewhat dependent on the platform and the type of link opened. So the function getaddressinfo allocates the structure for you and the function freeaddrinfo frees that allocated structure.

And that is why the pointer multiply dereference.

Look in the general case of passing a point of any type T to a function fn

void fn(T*);

It is normally for 1 of 2 reasons, ether T is so large you don't want to pass it by value (although I would normally expect the parameter to have type const T* in this case) or the function is going to change the value of a variable of type T in the calling code.

That holds for whatever type T is. If T is an int the calling code declares an int and passes a pointer to the int to the function

int a;

Fn can now alter the value of a in the calling code. Here type T is int, the function parameter has type int * and effectively an int has been passed back to the calling code.

If you want the called function to allocate some memory and pass a pointer to the allocated memory back to the calling code needs a pointer to receive the value the type of T is int * (for an integer pointer) and the function accepts int **. The function is not just going to write an integer, it is going to allocate memory for the integer, write the integer and return a pointer to the allocated integer.

In your case T is struct addressinfo*, that is the type you should be declaring in your calling code otherwise you are like to end up with undefined behaviour or a compiler error. In fact the function getaddressinfo may allocate a linked list of struct addressinfo and store the pointer to the head in your pointer.

It is a question of what is passed back by the function in this case what is passed back is a memory address, a pointer. To pass back a pointer you need a pointer to a pointer in the same way that to pass back an int you need a pointer to an int.

I have to say that I would not think I have every seen triple de-referencing used, at least not except in jest.
Dec 3 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.