473,799 Members | 2,837 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Usefulness of multiple levels of indirection.

Markus
6,050 Recognized Expert Expert
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,
Mark.
Dec 3 '09 #1
1 3455
Banfa
9,065 Recognized Expert Moderator Expert
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(&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

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

Similar topics

2
1433
by: NotGiven | last post by:
For example; Building One tenent 1234 Joe SMith tenent 3872 Martha Washington Building Eight tenent 2918 Stan Museal tenent 3458 Larry Lawrence tenent 2393 Kelly LeBlockney tenent 2091 George Money
2
12753
by: RJ | last post by:
We currently send product releases to our customers, and often have to include scripts that need to be ran on the Oracle databases (also do it for SqlServer customers, but we use a different set of scripts for that). Some of the Sql scripts can be quite long, and so we break them out to their own script files and call them all from one main ..sql file, using sqlplus to execute it all. The admin would run the file like:
2
1967
by: reclusive monkey | last post by:
I followed the instructions from the all-knowing Ms. Tennison here; http://www.biglist.com/lists/xsl-list/archives/200101/msg00070.html And it works great for the first two levels. However, when I tried to apply it to three levels, the third level just doesn't show. I am sure its something obvious but I just can't spot it. Can anyone see where I have gone wrong? Thanks in advance. Sample from XML file;
2
4664
by: James X. Li | last post by:
Is there a way to implement multiple login forms for ASP.NET applications? With our application we want to implement simple login form for normal resources (downloadable files), but more rigorous login forms for tasks like administration and configuration. The current ASP.NET seems only to allow to a single login form via the authentication element in web.config in the root directory. One work-around for our problem is implementing a...
0
1187
by: Skinnybobb | last post by:
Hiya, I have created a menu using the tab designer v2.0. I would like to create sub menus for the tabs. How do I do this?? Mark -- Skinnybobb ------------------------------------------------------------------------
2
3018
by: pengbsam | last post by:
Hello: I need to write a program that search through multiple level BOM, get all the items. It seems like a easy enough project, but when I put my hands on it and couple of hundred lines of codes later. It starts look more difficult. I was hopeing someone might have a very easy way to do it that I don't know about... The BOM structure looks like this.. Father Child A 1 A 2
13
2506
by: Karch | last post by:
I find myself doing things like this all the time: if ( SomeObject != null && SomeObject.AnotherObject != null && SomeObject.AnotherObject.YetAnother != null && SomeObject.AnotherObject.YetAnother.HelpMePlease != null) { // Do Something }
8
2236
by: Wes Groleau | last post by:
This file format has multiple levels (X12). One level could have one or more instances of the next level contained within it. Kind of like XML, except that some sections have no end tags, and the ones that do have end tags actually have a _different_ tag for the end. (ISA ...IEA or GS ... GE) It's easy enough to read a line at a time, see what type it is, and insert its parts into the appropriate table. Keeping track of the keys of...
3
1842
by: jonceramic | last post by:
Hi all, I'm not sure exactly what words to use or how to Google this. (I keep coming up with BOL links.) So, I'm going to ask the group for a starting point or proper terms to even describe what I'm doing. Is this a "thread handler? Or a "hierarchy tree" or what? ----------------------- Basically, I'm working on a project that will be a task and project tracking tool. What I've been asked to do is provide functionality
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10482
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10225
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.