Friday, April 19, 2024
HomeGolangWhat number of ranges of pointer indirection can you have got at...

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


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

I vaguely do not forget that there was some form of restrict to what number of time you may oblique an object, though I can not bear in mind in what context. Can anybody assist ?

I believe you may have as many ranges of pointer as you need, no less than till runtime security verify doesn’t panic on account of out of reminiscence. In fact to be able to learn the origin pointed worth it is advisable create the identical variety of variables known as a, b, c, and many others…every with a reference to the earlier. Is it actually helpful/clear/idiomatic?

Not less than 1024 :grin:Go Playground – The Go Programming Language

1 Like

Hello guys,

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

There was a put up, on this discussion board, final yr (months in the past) discussing this. There’s a restrict of two however I can not bear 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:

sort MyStruct struct {
    Field1 string
    Field2 int
}

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

That is the restrict I used to be fascinated by. So you may have an “infinite” pointer indirection, however you can not consumer the tackle operator twice in a row.

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

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

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

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

As a result of meaning to retailer 5 into i after which take the tackle of i and retailer it into p. You can not, nevertheless, do that:

var p *int = &5

Since you’re asking to take the tackle 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{...}. It is a particular case in Go that primarily interprets to:

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

I believe the language designers noticed using tips to structs as frequent sufficient that they have been OK with including the “magic” that permits you to take the tackle of a struct literal, however they didn’t go so far as permitting you to take the tackle of the tackle of a struct literal, identical to they don’t allow you to take the tackle 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’d actually prefer to see what you’re coding!

1 Like

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

Hey Everybody,
There is no such thing as a onerous 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 keep. Moreover, utilizing too many ranges of indirection can result in efficiency points, as every stage requires an extra reminiscence entry. It’s typically really useful to make use of pointer indirection sparingly and to carefull thought of 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!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments