In a C++ program that doesn't contain legacy C code, is there a guideline regarding the maximum number of levels of indirection that should be used in the source code? I know that in C (as opposed to C++), some programmers have used pointers to pointers for a multiple dimension array, but for the case of arrays, there are data structures in C++ that can be used to avoid the pointers to pointers.

Are users who still create pointers to pointers (or more than this) trying to use pointers to pointers only for performance ETC. reasons?

I have tried NOT to use any more than a pointer to a pointer, only in the case that a pointer needed modification; does anyone have any other official or unofficial guidelines or rules regarding the number of levels of indirection?

  • 4
    Just use whatever suits your needs. If you need no indirection, so be it. If you need 1 level of indirection, so be it. If you need N levels of indirection, so be it. – Thomas Eding Dec 18 '12 at 0:13
  • 5
    Remember: There are no programming problems that can't be solved by adding another layer of indirection... except for the problem of having too many layers of indirection! – Mason Wheeler Dec 18 '12 at 0:38
  • 2
    @MasonWheeler Actually in that case you can add a parallel set of indirection layers which is "shorter" than the problematic indirection tree, kinda like a vertical skip list. – Tacroy Dec 18 '12 at 0:42
  • Are there code examples showing the problem and/or backing the claim? Code examples can come from open-source projects. – rwong Dec 18 '12 at 4:42

There are no guidelines or any practical limits on indirection. There's no good reason to place a constraint like this unless you're regularly dealing with several levels of indirection in a case indicative of a sloppy design.

That is to say that there are valid cases where 4, 5, 6, or more levels of indirection is to be expected, but these are rare and should not typically appear in your code unless someone has done something terrible, not directly related to the level of indirection being employed.

If you are finding yourself using, say, 3 or more levels of indirection, then you should probably consider whether your architecture needs to be changed.

But does that mean you should never allow more than 3 levels of indirection? Absolutely not. It simply means that it might potentially be an indication that there could possibly be an inherent inefficiency in the architecture of your code. Nothing more. (Do note the deliberate vagueness of that statement: it is a mere possibility of something that could lead to a problem, not necessarily an actual problem.)

  • +1 For your effort making the tenuous link between bad design and indirection levels as clear as possible.... – mattnz Dec 18 '12 at 3:24

Note that a linked list of 42 elements contains 42 levels of indirection for accessing the last element. To get to the element of a one-element list, you use a single indirection: given a pointer to the list, you do something like p->data. To get to the second element, two indirections are required: p->next->data. Each arrow is an indirection. Only, the notation isn't just a row of ****'s. Of course, p->next is really (*p).next. So you could write that as (*(*(*p).next).next).data. If you want to see forty-two asterisks, that can be accomodated.

If the next field of the linked list is at the beginning of the structure, then at the machine level it really is just the equivalent of (***...***p).data, where only the final access to data uses an offset.

And of course, tree structures contain many levels of indirection, particularly if their fan-out is low, such as binary search trees, and particularly if they are allowed to become unbalanced.

  • 1
    the counter-argument here would be that you used a C example. In C++, you should define a class that abstracts away the complexity of linked list. And no function in implementation of that class or outside should have 42 levels. Better yet, if you have pure c++ code, why not use STL? Sorry, I'm sure you are just making an example, but this answer reminded me of people who truly "believe" they code in C++ but when I go into their code all I see is a whole bunch of C constructs (and not good ones at that) but they happened to use a class once or maybe cout instead of printf. – DXM Dec 18 '12 at 5:05
  • 2
    @DXM The abstraction doesn't make the indirection go away, it just hides it. And that's important answer to the question: it's not how many levels of indirection there are that matters, it's how many levels are exposed and how many you're expected to use at one time. – Caleb Dec 18 '12 at 5:09
  • @Caleb: I assumed OP was asking the question from a coding convention/style point of view. i.e. when I read/write code, what's the max number of stars should I expect to find in front of a variable name? If we are talking from a purely theoretical/computer science perspective, then sure, every link is an indirection. – DXM Dec 18 '12 at 5:12

According to MISRA C++:2008, rule 5-0-19:

The declaration of objects shall contain no more than two levels of pointer indirection.

Although that's only a declaration restriction. You could still access a variable though any number of indirections.


It is EXTREMELY rare that it is actually a good idea to write code with more than one level of indirection at any given instant.

Part of this is because prudence dictates that you check every pointer for NULL before you dereference it, and that's hard to do if you write more than one level of indirection.

Part of this is because modern processors have complicated data cache, and even more complicated memory allocation schemes, and walking multiple levels of indirection (through long pointer chains) can thrash the holy crap out of the cache.

I have seen processors that can deliver up erroneous results if you thrash the cache in certain ways. (Yes, these are hardware problems. No, the vendor is NOT going to fix them any time soon, and in any case those chips are already out in the field.) I personally fixed a problem, that smelled like one of those, on a processor for which the manufacturer had already warned about issues like this, by rewriting the code to be very careful about locality of references. (I did not do the rewrite because I suspected a thrashed cache. I found out about that issue later. I did the rewrite because the original code was an unreadable pile of spaghetti and pointers to pointers to pointers. I was mildly perturbed when the problem I was chasing just vanished swiftly and silently away.)

AND part of it is because code that is only doing one level of indirection is usually a LOT easier for the poor maintenance schlub to understand.

  • "It is EXTREMELY rare that it is actually a good idea to write code with more than one level of indirection at any given instant." (Most) Every time you access an object that is garbage collected, you are using 2 levels of indirection... – Thomas Eding Dec 18 '12 at 18:35
  • More than two is extremely rare. Two is quite common, allocating a pointer and returning it to the caller via a pointer argument instead of a reference. – gnasher729 May 29 '19 at 14:12
  • 'It is EXTREMELY rare that it is actually a good idea to write code with more than one level of indirection at any given instant.' matrices like A[i][j] are abad idea? – jinawee May 20 '20 at 16:36
  • @jinawee, if you are writing code involving references to matrices and matrix elements, you should be writing that code as though it was actually using matrices and matrix elements, as opposed to playing pointer games. Depending on exactly what you are doing, you should probably consider using a language that actually knows what matrices are (like modern FORTRAN, PASCAL, Ada, or MATLAB), as opposed to something that doesn't really, like C or C++. IN PARTICULAR, you should NOT be unrolling the loops and playing pointer games. Let the compiler do that: he can do it safely. You might not. – John R. Strohm May 21 '20 at 0:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.