pointers, assignments, Garbage Collection Oh My?

Maxim Fomin maxim at maxim-fomin.ru
Thu Jul 11 10:20:16 PDT 2013


On Thursday, 11 July 2013 at 17:07:18 UTC, Ali Çehreli wrote:
> On 07/11/2013 12:23 AM, Jacob Carlborg wrote:
>
> > On 2013-07-10 20:22, Ali Çehreli wrote:
> >
> >> And to be pedantic, length comes first:
> >>
> >> struct Array (T)
> >> {
> >>      size_t length;
> >>      T* ptr;
> >> }
> >
> > I thought "ptr" came first, that's the reason you could cast
> to the
> > pointer type. Not that one should do that. Perhaps there's
> some
> > compiler/runtime magic involved.
>
> There must be little magic and that magic should be the same as 
> getting the .ptr property. Otherwise, the "value" of a struct 
> object cannot be casted to pointer type:
>
> struct S
> {
>     int *p;
> }
>
>     auto s = S();
>     int *p = cast(int*)s;
>
> Error: e2ir: cannot cast s of type S to type int*
>
> Ali

In context of slices cast(int*)arr is essentially s.ptr. There is 
no magic but accessing right field of struct.


More information about the Digitalmars-d-learn mailing list