On Fri, Aug 18, 2023 at 8:12 AM Tibor Halter <
tha...@zupa.hu> wrote:
>
> Hi Elias, thanks for chiming in!
>
> > The Go garbage collector is precise in that it only considers pointers when determining memory that can be freed.
>
> Agreed, but it is not as precise on _what_ is a pointer. Specifically, which matters?
> - the type used upon allocating the memory, or
> - the type used upon writing to it.
>
> Because *T or unsafe.Pointer is required upon writing to a memory location for it to be marked as a pointer, I came to the conclusion that the type upon writing is what matters.
>
> So then both types need to be pointers?
>
> Does that mean there is no way to define a struct-like memory layout dynamically, that contains a mix of pointer and non-pointer types, in a way that is supported by the GC?
> (This is what I'm trying to do.)
The rule is pretty simple: a pointer must always have a pointer type.
That includes when it is in a variable and when it is stored in
memory. In your terms, what matters is the type used upon allocating
the memory.
Your code is not following any of the patterns permitted for
unsafe.Pointer (any patterns that are not explicitly permitted are
forbidden). The code is effectively converting from *byte to *Num.
As it says at
https://pkg.go.dev/unsafe#Pointer that kind of
conversion is only permitted if the types in question "share an
equivalent memory layout". byte and Num have different memory
layouts.
You can define a struct-like memory layout dynamically by using
reflect.StructOf.
Ian
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
golang-nuts...@googlegroups.com.
> To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/5051da1c-36de-4699-906c-ff8f50754404n%40googlegroups.com.