Skip to main content
gamedev.net
🔒 Locked

Three-level pointer indirection - any usages?

Started by uutee 12 replies 2.4k views
Original Post
uutee
uutee
Hi, I've been playing around with this theory that the reason that most coders are accustomed to at max two-level pointer indirection, e.g. void safe_delete( int** p ) { delete *p; *p = 0; } is that the "practical world" doesn't expose us to problems that require more than two-level pointer indirection. But then again, having an intuitive grasp of multi-level pointer indirection, might embetter one as a programmer. So I thought: perhaps one could come up with practical examples of three-level pointer indirection. So basically I'm asking; have you ever seen (or have come up with) code that practically requires something like "int*** p", assuming this is just a pointer-to-pointer-to-pointer-to-integer, and has nothing to do with arrays? Thanks, - Mikko
White Scorpion
White Scorpion
Most of the time there is a better alternative; I can't really think of a situation where this syntax is useful.
doynax
doynax
It happens, although it's not exactly common. But the first few levels will generally be wrapped up in a typedef or structure anyway, so you're still not doing it conceptually.
As for concrete examples, I can't think of anything off-hand..
Erzengeldeslichtes
Erzengeldeslichtes
void WorkOnStaticArray(int*** Array){  Array[1][2][3] = 2;}


[wink]
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
kSquared
kSquared
Dereferencing an element in a list of handles might be one possible use. But I'll admit that's a bit contrived.
- k2 "Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ<
Illco
Illco
Can't think of anything besides kSquared's example. But conceptually there isn't that much difference between single-, double- or multi-level indirection anyway.

Illco
SiCrane
SiCrane
I don't think I've ever seen more than two levels of indirection unless there was an array in there somewhere. Admittedly, I've seen some really ugly indirections involving arrays.
SirLuthor
SirLuthor
The only times I ever used double-indirection with pointers is when I need to pass a pointer into a creation function, and have it point to the created object, and even in that case, I tend to pass a reference to a pointer instead or a pointer to pointer, as it makes things easier to read and understand, I feel. I can't think of an example that would use triple indirection offhand.
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
JohnBolton
JohnBolton
A contrived-but-not-unreasonable example:
    int * bar[BAR_SIZE];    void GetBar( int *** foo )    {        *foo = bar;    } 
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
furby100
furby100
2D array of pointers is one. In fact, if you wanted to pass a the pointer to the 2D array of pointers to a function so that it would be altered to point to a different 2D array of pointers you could have quadruple indirection.

Above double indirection it becomes progressively more irritating to allocate and deallocate with nested loops and so forth though, so this is a further reason (other than the dereferencing confusion) why it is not normally done.
SiCrane
SiCrane
The three star programmers link reminds me of some code I had to deal with once. I'm pretty sure it went like this:

void (* const * volatile * table(void) )(void) ;

It's a function that returns a pointer to a volatile pointer to an array of const pointers to void(void) functions. It was written by a guy who didn't quite grok DLLs.
iMalc
iMalc
I have seen it twice. It was to do with an IUnknown* in COM, with two more levels of indirection for various reasons.
The other time was when I tried writing a pool allocator, though I vastly simplified it shortly after such that it barely used two levels.

In probably shouldn't ever be necessary, but if it is used, then it probably can at least be changed to a reference to a pointer-pointer (**&) instead to make it easier.
d000hg
d000hg
Yeah, a 3D array would do it.
Or a 2D array of COM objects.
If you were using ASM to look at some C code of structs containing other structs it must happen all the time?

I don't see why n-level indirection is hard to understand, it just gets easy to get lost. But the theory is no different.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.