477

How many pointers (*) are allowed in a single variable?

Let's consider the following example.

int a = 10;
int *p = &a;

Similarly we can have

int **q = &p;
int ***r = &q;

and so on.

For example,

int ****************zz;
17
  • 4
    Technically there is probably no limit (although that is compiler specific). The real limit is your (and your coworkers') mental capacity to keep track of what your variable actually represents. Making and keeping the code readable is usually more important than low level optimization. From this point of view, in production code already triple indirection (e.g. int***) is discouraged in most cases. Commented Apr 10, 2012 at 10:40
  • 299
    You can keep adding levels of pointers until your brain explodes or the compiler melts - whichever happens soonest.
    – JeremyP
    Commented Apr 10, 2012 at 10:52
  • 54
    Since a pointer to a pointer is again, well, just a pointer, there shouldn't be any theoretical limit. Maybe the compiler won't be able to handle it beyond some ridiculously high limit, but well... Commented Apr 10, 2012 at 10:59
  • 79
    with the newest c++ you should use something like std::shared_ptr<shared_ptr<shared_ptr<...shared_ptr<int>...>>>
    – josefx
    Commented Apr 10, 2012 at 14:09
  • 52
    @josefx - this shows a problem in the C++ standard - there's no way to raise smart pointers to powers. We must immediately demand an extension to support e.g. (pow (std::shared_ptr, -0.3))<T> x; for -0.3 levels of indirection.
    – user180247
    Commented Apr 11, 2012 at 0:05

15 Answers 15

419

The C standard specifies the lower limit:

###5.2.4.1 Translation limits

276 The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits: [...]

279 — 12 pointer, array, and function declarators (in any combinations) modifying an arithmetic, structure, union, or void type in a declaration

The upper limit is implementation specific.

10
  • 127
    The C++ standard "recommends" that an implementation support at least 256. (Readability recommends that you not exceed 2 or 3, and even then: more than one should be exceptional.) Commented Apr 10, 2012 at 10:52
  • 24
    This limit is about how many in a single declaration; it does not impose an upper bound on how much indirection you can achieve via multiple typedefs.
    – Kaz
    Commented Apr 10, 2012 at 11:37
  • 13
    @Kaz - yes, that is true. But since the spec is (no pun intended) specifying a mandatory lower limit, it is the effective upper bound all spec-compliant compilers are required to support. It could be lower than the vendor-specific upper bound, of course. Rephrased differently (to align it with the OP's question), it is the maximum allowed by the spec (anything else would be vendor-specific.) A bit off the tangent, programmers should (at least in the general case) treat that as their upper limit (unless they have a valid reason to rely on a vendor-specific upper bound)... me thinks. Commented Apr 10, 2012 at 16:00
  • 5
    On another note, I would start to cut myself if I had to work with code that had long-ass dereferencing chains (specially when liberally peppered all over the place.) Commented Apr 10, 2012 at 16:03
  • 12
    @beryllium: Usually these numbers come from taking a survey of pre-standardization software. In this case presumably they looked at common C programs and existent C compilers, and found at least one compiler that would have trouble with more than 12 and/or no programs that broke if you restricted it to 12.
    – user79758
    Commented Apr 15, 2012 at 14:48
164

Actually, C programs commonly make use of infinite pointer indirection. One or two static levels are common. Triple indirection is rare. But infinite is very common.

Infinite pointer indirection is achieved with the help of a struct, of course, not with a direct declarator, which would be impossible. And a struct is needed so that you can include other data in this structure at the different levels where this can terminate.

struct list { struct list *next; ... };

now you can have list->next->next->next->...->next. This is really just multiple pointer indirections: *(*(..(*(*(*list).next).next).next...).next).next. And the .next is basically a noop when it's the first member of the structure, so we can imagine this as ***..***ptr.

There is really no limit on this because the links can be traversed with a loop rather than a giant expression like this, and moreover, the structure can easily be made circular.

Thus, in other words, linked lists may be the ultimate example of adding another level of indirection to solve a problem, since you're doing it dynamically with every push operation. :)

14
  • 52
    That's a completely different issue, though - a struct containing a pointer to another struct is very different than a pointer-pointer. An int***** is a distinct type from an int****.
    – fluffy
    Commented Apr 11, 2012 at 17:22
  • 16
    It is not "very" different. The difference is fluffy. It is closer to syntax than semantics. A pointer to a pointer object, or a pointer to a structure object which contains a pointer? It's the same kind of thing. Getting to the tenth element of a list is ten levels of addressing indirection. (Of course, the ability to express an infinite structure depends on the struct type being able to point to itself via the incomplete struct type so that list->next and list->next->next are the same type; otherwise we would have to construct an infinite type.)
    – Kaz
    Commented Apr 11, 2012 at 18:28
  • 41
    I didn't consciously notice that your name is fluffy when I used the word "fluffy". Subsconscious influence? But I am sure I have used the word in this way before.
    – Kaz
    Commented Apr 11, 2012 at 18:29
  • 4
    Also remember that in machine language, you could just iterate on something like LOAD R1, [R1] as long as R1 is a valid pointer at every step. There are no types involved other than "word which holds an address". Whether or not there are declared types doesn't determine the indirection and how many levels it has.
    – Kaz
    Commented Apr 12, 2012 at 3:56
  • 4
    Not if the structure is circular. If R1 holds the address of a location which points to itself then LOAD R1, [R1] can be executed in an infinite loop.
    – Kaz
    Commented Apr 19, 2012 at 19:11
84

Theoretically:

You can have as many levels of indirections as you want.

Practically:

Of course, nothing that consumes memory can be indefinite, there will be limitations due to resources available on the host environment. So practically there is a maximum limit to what an implementation can support and the implementation shall document it appropriately. So in all such artifacts, the standard does not specify the maximum limit, but it does specify the lower limits.

Here's the reference:

C99 Standard 5.2.4.1 Translation limits:

— 12 pointer, array, and function declarators (in any combinations) modifying an arithmetic, structure, union, or void type in a declaration.

This specifies the lower limit that every implementation must support. Note that in a footenote the standard further says:

18) Implementations should avoid imposing fixed translation limits whenever possible.

8
  • 17
    indirections don't overflow any stacks! Commented Apr 10, 2012 at 10:37
  • 1
    Corrected, I had this errie feeling of reading & answering the q as limit of parameters being passed to the function. I don't know why?!
    – Alok Save
    Commented Apr 10, 2012 at 10:39
  • 2
    @basile - I'd expect stack-depth to be an issue in the parser. Many formal parsing algorithms have a stack as a key component. Most C++ compilers probably use a variant of recursive descent, but even that relies on the processor stack (or, pedantically, on the language acting as if there was a processor stack). More nesting of grammar rules means a deeper stack.
    – user180247
    Commented Apr 11, 2012 at 0:14
  • 9
    indirections don't overflow any stacks!--> No! parser stack can overflow. How does the stack relate to pointer indirection? Parser stack! Commented Apr 11, 2012 at 7:15
  • If * is overloaded for a number of classes in a row, and each overload returns an object of other type in the row, then there can be stackoverflow for such chained function calls. Commented May 27, 2012 at 5:10
80

As people have said, no limit "in theory". However, out of interest I ran this with g++ 4.1.2, and it worked with size up to 20,000. Compile was pretty slow though, so I didn't try higher. So I'd guess g++ doesn't impose any limit either. (Try setting size = 10 and looking in ptr.cpp if it's not immediately obvious.)

g++ create.cpp -o create ; ./create > ptr.cpp ; g++ ptr.cpp -o ptr ; ./ptr

create.cpp

#include <iostream>

int main()
{
    const int size = 200;
    std::cout << "#include <iostream>\n\n";
    std::cout << "int main()\n{\n";
    std::cout << "    int i0 = " << size << ";";
    for (int i = 1; i < size; ++i)
    {
        std::cout << "    int ";
        for (int j = 0; j < i; ++j) std::cout << "*";
        std::cout << " i" << i << " = &i" << i-1 << ";\n";
    }
    std::cout << "    std::cout << ";
    for (int i = 1; i < size; ++i) std::cout << "*";
    std::cout << "i" << size-1 << " << \"\\n\";\n";
    std::cout << "    return 0;\n}\n";
    return 0;
}
2
  • 77
    I couldn't get more than 98242 when I tried it. (I did the script in Python, doubling the number of * until I got one that failed, and the preceding one that passed; I then did a binary search over that interval for the first one that failed. The whole test took less than a second to run.) Commented Apr 10, 2012 at 11:31
  • For 200_000, the ptr.cpp file weighs 19G, and 150G of RAM is not enough to compile it.
    – Lem
    Commented Jan 25 at 18:37
64

Sounds fun to check.

  • Visual Studio 2010 (on Windows 7), you can have 1011 levels before getting this error:

    fatal error C1026: parser stack overflow, program too complex

  • gcc (Ubuntu), 100k+ * without a crash ! I guess the hardware is the limit here.

(tested with just a variable declaration)

1
  • 5
    Indeed, the productions for unary operators are right-recursive, which means that a shift-reduce parser will shift all of the * nodes onto the stack before being able to make a reduction.
    – Kaz
    Commented Apr 10, 2012 at 19:15
30

There is no limit, check example at Pointers :: C Interview Questions and Answers.

The answer depends on what you mean by "levels of pointers." If you mean "How many levels of indirection can you have in a single declaration?" the answer is "At least 12."

int i = 0;

int *ip01 = & i;

int **ip02 = & ip01;

int ***ip03 = & ip02;

int ****ip04 = & ip03;

int *****ip05 = & ip04;

int ******ip06 = & ip05;

int *******ip07 = & ip06;

int ********ip08 = & ip07;

int *********ip09 = & ip08;

int **********ip10 = & ip09;

int ***********ip11 = & ip10;

int ************ip12 = & ip11;

************ip12 = 1; /* i = 1 */

If you mean "How many levels of pointer can you use before the program gets hard to read," that's a matter of taste, but there is a limit. Having two levels of indirection (a pointer to a pointer to something) is common. Any more than that gets a bit harder to think about easily; don't do it unless the alternative would be worse.

If you mean "How many levels of pointer indirection can you have at runtime," there's no limit. This point is particularly important for circular lists, in which each node points to the next. Your program can follow the pointers forever.

7
  • 7
    There's almost certainly a limit, since the compiler has to keep track of the information in a finite amount of memory. (g++ aborts with an internal error at 98242 on my machine. I expect that the actual limit will depend on the machine and the load. I also don't expect this to be a problem in real code.) Commented Apr 10, 2012 at 11:12
  • 2
    Yeah @MatthieuM. : I just considered theoretically :) Thanks James for completing answer Commented Apr 10, 2012 at 11:20
  • 3
    Well, linked lists aren't really a pointer to a pointer, they're a pointer to a struct that contains a pointer (either that or you end up doing a lot of unnecessary casting)
    – Random832
    Commented Apr 10, 2012 at 13:58
  • 1
    @Random832: Nand said 'If you mean "How many levels of pointer indirection can you have at runtime,"' so he was explicitly removing the restriction of just talking about pointers to pointers (*n).
    – LarsH
    Commented Apr 10, 2012 at 15:46
  • 1
    I don't get your point: 'There is no limit, check example here.' The example is no proof that there is no limit. It only proves that a 12 star indirection is possible. Neither proves anything the circ_list example regarding the OP's question: The fact that you can traverse a pointers list doesn't imply that the compiler can compile a n-stars indirection.
    – Alberto
    Commented Apr 11, 2012 at 14:18
26

It's actually even funnier with pointer to functions.

#include <cstdio>

typedef void (*FuncType)();

static void Print() { std::printf("%s", "Hello, World!\n"); }

int main() {
  FuncType const ft = &Print;
  ft();
  (*ft)();
  (**ft)();
  /* ... */
}

As illustrated here this gives:

Hello, World!
Hello, World!
Hello, World!

And it does not involve any runtime overhead, so you can probably stack them as much as you want... until your compiler chokes on the file.

0
21

There is no limit. A pointer is a chunk of memory whose contents are an address.
As you said

int a = 10;
int *p = &a;

A pointer to a pointer is also a variable which contains an address of another pointer.

int **q = &p;

Here q is pointer to pointer holding the address of p which is already holding the address of a.

There is nothing particularly special about a pointer to a pointer.
So there is no limit on chain of poniters which are holding the address of another pointer.
ie.

 int **************************************************************************z;

is allowed.

0
19

Every C++ developer should have heard of the (in)famous Three star programmer.

And there really seems to be some magic "pointer barrier" that has to be camouflaged.

Quote from C2:

Three Star Programmer

A rating system for C-programmers. The more indirect your pointers are (i.e. the more "*" before your variables), the higher your reputation will be. No-star C-programmers are virtually non-existent, as virtually all non-trivial programs require use of pointers. Most are one-star programmers. In the old times (well, I'm young, so these look like old times to me at least), one would occasionally find a piece of code done by a three-star programmer and shiver with awe. Some people even claimed they'd seen three-star code with function pointers involved, on more than one level of indirection. Sounded as real as UFOs to me.

0
13

Note that there are two possible questions here: how many levels of pointer indirection we can achieve in a C type, and how many levels of pointer indirection we can stuff into a single declarator.

The C standard allows a maximum to be imposed on the former (and gives a minimum value for that). But that can be circumvented via multiple typedef declarations:

typedef int *type0;
typedef type0 *type1;
typedef type1 *type2; /* etc */

So ultimately, this is an implementation issue connected to the idea of how big/complex can a C program be made before it is rejected, which is very compiler specific.

6

I'd like to point out that producing a type with an arbitrary number of *'s is something that can happen with template metaprogramming. I forget what I was doing exactly, but it was suggested that I could produce new distinct types that have some kind of meta maneuvering between them by using recursive T* types.

Template Metaprogramming is a slow descent into madness, so it is not necessary to make excuses when generating a type with several thousand level of indirection. It's just a handy way to map peano integers, for example, onto template expansion as a functional language.

0
2

Rule 17.5 of the 2004 MISRA C standard prohibits more than 2 levels of pointer indirection.

2
  • 19
    Pretty sure that's a recommendation for programmers, not for compilers.
    – Cole Tobin
    Commented Feb 7, 2014 at 2:16
  • 4
    I read the document with rule 17.5 about more than 2 levels of pointer indirection. And it doesn't necessarily prohibit more than 2 levels. It does state that the ruling should be followed as more than 2 levels is "non-compliant" to their standards. The important word or phrase in their ruling is the use of the word "should" from this statement: Use of more than 2 levels of indirection can seriously impair the ability to understand the behavior of the code, and should therefore be avoided. These are guidelines set by this organization as opposed to rules set by the language standard. Commented Nov 24, 2017 at 4:48
1

The limits vary quite drastically between languages: 256 in C++, 12 in C. Note that the limit not only applies to how many pointers there can be in a declarator; it also applies to function, array, etc. declarators.

C++23

The minimum limit can be found in Annex B - Implementation quantities .

Pointer, pointer-to-member, array, and function declarators (in any combination) modifying a type in a declaration [256].

- [implimits] p2.3

C17

The minimum limit can be found in 5.2.4.1 Translation limits.

12 pointer, array, and function declarators (in any combinations) modifying an arithmetic, structure, union, or void type in a declaration

- 5.2.4.1 Translation limits, p1


Both limits are minimums. The exact limit is implementation-defined and may be greater.

-3

There isn't such a thing like real limit but limit exists. All pointers are variables that are usually storing in stack not heap. Stack is usually small (it is possible to change its size during some linking). So lets say you have 4MB stack, what is quite normal size. And lets say we have pointer which is 4 bytes size (pointer sizes are not the same depending on architecture, target and compiler settings).

In this case 4 MB / 4 b = 1024 so possible maximum number would be 1048576, but we shouldn't ignore the fact that some other stuff is in stack.

However some compilers may have maximum number of pointer chain, but the limit is stack size. So if you increase stack size during linking with infinity and have machine with infinity memory which runs OS which handles that memory so you will have unlimited pointer chain.

If you use int *ptr = new int; and put your pointer into heap, that is not so usual way limit would be heap size, not stack.

EDIT Just realize that infinity / 2 = infinity. If machine has more memory so the pointer size increases. So if memory is infinity and size of pointer is infinity, so it is bad news... :)

5
  • 5
    A) Pointers can be stored on the heap (new int*). B) An int* and an int********** have the same size, at least on reasonable architectures.
    – user142019
    Commented Jul 30, 2013 at 7:23
  • @rightfold A) Yes pointers can be stored in heap. But it would be very different thing like creating container which hold pointers what are pointing to the next previous pointer. B) Of course int* and an int********** have the same size, I didn't said that they have different.
    – ST3
    Commented Jul 30, 2013 at 7:28
  • 3
    Then I don't see how stack size is even remotely relevant.
    – user142019
    Commented Jul 30, 2013 at 7:28
  • @rightfold I've been thinking about usual way of data distribution when all data is in heap and on stack it is just pointers to that data. It would be usual way, but I agree that it is possible to put pointers in stack.
    – ST3
    Commented Jul 30, 2013 at 7:31
  • "Of course int* and an int********** have the same size" - the standard doesn't guarantee that (although I know of no platform where it isn't true). Commented Mar 22, 2018 at 9:12
-4

It depends on the place where you store pointers. If they are in stack you have quite low limit. If you store it in heap, you limit is much much much higher.

Look at this program:

#include <iostream>

const int CBlockSize = 1048576;

int main() 
{
    int number = 0;
    int** ptr = new int*[CBlockSize];

    ptr[0] = &number;

    for (int i = 1; i < CBlockSize; ++i)
        ptr[i] = reinterpret_cast<int *> (&ptr[i - 1]);

    for (int i = CBlockSize-1; i >= 0; --i)
        std::cout << i << " " << (int)ptr[i] << "->" << *ptr[i] << std::endl;

    return 0;
}

It creates 1M pointers and at the shows what point to what it is easy to notice what the chain goes to the first variable number.

BTW. It uses 92K of RAM so just imagine how deep you can go.

0

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