Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

As we all know. We can have pointer to a pointer to a pointer..
But here i would like to know. Till what extent we can have this chain?

like
pointer to a pointer to a pointer to a pointer ... so on.


Obviously there will be some limit. But What is that and more important is Why?


Thanks in advance
Posted
Comments
enhzflep 16-Aug-12 8:57am    
With the right mechanism to traverse the chain of pointers, you could have a chain that was long enough to use up all available memory.
1,000 links only uses 4kb of mem in a 32-Bit system, 8kb in 64Bit system.
A couple of gig is an awful lot of 4 or 8 byte chunks. :-)

You could think of such a system as being like a linked list - the key difference being that in this case, each node holds no data other than the address of the next node. The actual data being pointed to is a special case - you've no way of differentiating it from a pointer. You'd have to keep a count of the number of 'links' you had to traverse till you got to your actual data.

In addition to the comment of enhzflep, It could also easily be endless if the first pointer points to a second, and this second pointer points back to the first.

Also, it isn't very useful to have a lot of pointers in a long chain in just a single block of code. But different libraries together can chain up pointers to a function. Bios methods are a good example because there is a pointer table on a fixed address which again point to bios routines that might depend from system to system. Your program will get a pointer to the pointer of that bios routine, but the nice thing is that from your code's perspective it's just a single pointer. Same goes with dll's by the way. This way it can relocate to any position without breaking the program (but the relocation table is almost never used). For pure data objects/structures it is doubtful to chain a lot of pointers because it doesn't help. Some redirection is helpful but at a certain point its just memory pollution and simply unnecessary.

Good luck!
 
Share this answer
 
v2
Comments
Mihai Vrinceanu 17-Aug-12 11:26am    
void* p = &p;
Useless, but compiles.
Till what extent we can have this chain?


Well, the answer is "you can have as long a chain as you like until it reaches such a length that the code is unreadable and your employer fires you".

Chaining pointers beyond 2 or 3 is a sign of really bad data structure design. Also, when your code uses such a construct, you are really showing how much you've exposed the inner workings of a structure to the code. Oh, I agree that some APIs, DLLs, and/or BIOS calls need you to do this kind of thing but you really shouldn't be creating long chains for your own code.

While your question was about "C" and not "C++", good programming practices should apply and you should be encapsulating the "depth" of the pointers in a routine (in C) or an object (in C++).

And even in C++, it can get pretty ugly.
C++
obj->GetCust()->GetAcct()->GetBalanceDue()
is just bad encapsulation and is a good example of how to write bad C in C++.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900