Sunday, September 24, 2023
HomeGolangWhat number of ranges of pointer indirection can you've got at runtime?...

What number of ranges of pointer indirection can you’ve got at runtime? Is there no restrict? – Getting Assist

What number of ranges of pointer indirection can you’ve got at runtime? Is there no restrict ?

I vaguely do not forget that there was some type of restrict to what number of time you’ll be able to oblique an object, though I can’t keep in mind in what context. Can anybody assist ?

I believe you’ll be able to have as many ranges of pointer as you need, at the least till runtime security test doesn’t panic as a consequence of out of reminiscence. After all with a view to learn the origin pointed worth it’s essential create the identical variety of variables referred to as a, b, c, and so on…every with a reference to the earlier. Is it actually helpful/clear/idiomatic?

At the least 1024 :grin:Go Playground – The Go Programming Language

1 Like

Hello guys,

I vaguely keep in mind there’s such a limitation in Go, clearly it’s not about pointers. Then what’s it about ? Perhaps a pointer to an interface, or interface casting associated.

There was a put up, on this discussion board, final 12 months (months in the past) discussing this. There’s a restrict of two however I can’t keep in mind for what.

Hello all, however particularly @skillian and @Metalymph

I simply remembered the notions I used to be complicated. Have a look at this code:

kind MyStruct struct {
    Field1 string
    Field2 int

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

That is the restrict I used to be interested by. So you’ll be able to have an “infinite” pointer indirection, however you can not person the deal with operator twice in a row.

By the best way, why is that ? Extra particulars please.

Why is it, that in your playground instance, the restrict is 1023 ? why can I not oblique 1024 instances ?

The & operator takes the deal with of its operand, however that solely works if the worth you’re taking the deal with of is addressable. So you are able to do this:

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

As a result of which means to retailer 5 into i after which take the deal with of i and retailer it into p. You can’t, nonetheless, do that:

var p *int = &5

Since you’re asking to take the deal with of 5, however what would that even imply? The pointer has to level to one thing.

There’s an exception to this: var s *MyStruct = &MyStruct{...}. This can be a particular case in Go that basically interprets to:

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

I believe the language designers noticed using tips to structs as widespread sufficient that they had been OK with including the “magic” that lets you take the deal with of a struct literal, however they didn’t go so far as permitting you to take the deal with of the deal with of a struct literal, similar to they don’t allow you to take the deal with of different literal values.

There are 1024 indirections: The ultimate indirection is saved into p itself. You possibly can maintain copying and pasting the *s and increment the rely to as many as you need so far as I can inform. I meant for the instance to be foolish, although. In the event you want 1024+ indirections, I believe we might actually wish to see what you’re coding!

1 Like

so would I, more often than not I mess around philosophical questions.

Hey Everybody,
There isn’t a exhausting restrict on the variety of ranges of pointer indirection that can be utilized at runtime in most programming languages. Nonetheless, every stage of indirection provides a layer of complexity to the code, and utilizing too many ranges could make the code obscure and preserve. Moreover, utilizing too many ranges of indirection can result in efficiency points, as every stage requires a further reminiscence entry. It’s usually really helpful to make use of pointer indirection sparingly and to carefull thought-about the design of the code when utilizing a number of stage of indication.

I Hope You perceive it. Be happy to contact me for one more querry. Thanks!

The variety of ranges of pointer indirection which you could have at runtime relies on the precise programming language, compiler, and {hardware} structure you’re working with. In most sensible situations, there’s a sensible restrict to the variety of ranges of indirection as a consequence of components reminiscent of reminiscence constraints and the constraints of the {hardware} or compiler. Nonetheless, the precise restrict can fluctuate.

For instance, in languages like C and C++, the C customary doesn’t outline a selected restrict on the variety of ranges of indirection. It in the end relies on the obtainable reminiscence and the power of the {hardware} and compiler to deal with the indirection.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments