How many levels of pointer indirection can you have at runtime? Is there no limit?

How many levels of pointer indirection can you have at runtime? Is there no limit ?

I vaguely remember that there was some sort of limit to how many time you can indirect an object, although I cannot remember in what context. Can anyone help ?

I think you can have as many levels of pointer as you want, at least until runtime safety check doesn’t panic due to out of memory. Of course in order to read the origin pointed value you need to create the same number of variables called a, b, c, etc…each with a reference to the previous. Is it really useful/clear/idiomatic?

At least 1024 :grin: Go Playground - The Go Programming Language


Hi guys,

I vaguely remember there is such a limitation in Go, clearly it is not about pointers. Then what is it about ? Maybe a pointer to an interface, or interface casting related.

There was a post, on this forum, last year (months ago) discussing this. There is a limit of 2 but I cannot remember for what.

Hi all, but especially @skillian and @Metalymph

I just remembered the notions I was confusing. Look at this code:

type MyStruct struct {
    Field1 string
    Field2 int

var s *MyStruct = &MyStruct{"hello", 42} // compiles
var t **MyStruct = &&MyStruct{"hello", 42} // does NOT compile
var u **MyStruct = &(&MyStruct{"hello", 42}) // does NOT compile

This is the limit I was thinking about. So you can have an “infinite” pointer indirection, but you cannot user the address operator twice in a row.

By the way, why is that ? More details please.

Why is it, that in your playground example, the limit is 1023 ? why can I not indirect 1024 times ?

The & operator takes the address of its operand, but that only works if the value you’re taking the address of is addressable. So you can do this:

var i int = 5
var p *int = &i

Because that means to store 5 into i and then take the address of i and store it into p. You cannot, however, do this:

var p *int = &5

Because you’re asking to take the address of 5, but what would that even mean? The pointer has to point to something.

There’s an exception to this: var s *MyStruct = &MyStruct{...}. This is a special case in Go that essentially translates to:

var s *MyStruct = new(MyStruct)
*s = MyStruct{"hello", 42}

I suspect the language designers saw the use of pointers to structs as common enough that they were OK with adding the “magic” that allows you to take the address of a struct literal, but they did not go as far as allowing you to take the address of the address of a struct literal, just like they don’t let you take the address of other literal values.

There are 1024 indirections: The final indirection is stored into p itself. You could keep copying and pasting the *s and increment the count to as many as you want as far as I can tell. I meant for the example to be silly, though. If you need 1024+ indirections, I think we would really like to see what you’re coding!

1 Like

so would I, most of the time I play around philosophical questions.

Hey Everyone,
There is no hard limit on the number of levels of pointer indirection that can be used at runtime in most programming languages. However, each level of indirection adds a layer of complexity to the code, and using too many levels can make the code difficult to understand and maintain. Additionally, using too many levels of indirection can lead to performance issues, as each level requires an additional memory access. It is generally recommended to use pointer indirection sparingly and to carefull considered the design of the code when using multiple level of indication.

I Hope You understand it. Feel free to contact me for another querry. Thank you!

The number of levels of pointer indirection that you can have at runtime depends on the specific programming language, compiler, and hardware architecture you are working with. In most practical scenarios, there is a practical limit to the number of levels of indirection due to factors such as memory constraints and the limitations of the hardware or compiler. However, the specific limit can vary.

For example, in languages like C and C++, the C standard does not define a specific limit on the number of levels of indirection. It ultimately depends on the available memory and the ability of the hardware and compiler to handle the indirection. Regards

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.