Thread overview
Template!(X).sizeof bug
Jul 15, 2004
Matthew Wilson
Jul 17, 2004
Walter
Jul 17, 2004
Matthew
Jul 17, 2004
Matthew
Jul 17, 2004
Andy Friesen
Jul 17, 2004
Matthew
Jul 17, 2004
Walter
July 15, 2004
It always reports 4, irrespective of how many members I load into it.



July 17, 2004
"Matthew Wilson" <admin.hat@stlsoft.dot.org> wrote in message news:cd54dj$1le0$1@digitaldaemon.com...
> It always reports 4, irrespective of how many members I load into it.

Example, please.


July 17, 2004
"Walter" <newshound@digitalmars.com> wrote in message news:cda7v0$sd2$3@digitaldaemon.com...
>
> "Matthew Wilson" <admin.hat@stlsoft.dot.org> wrote in message news:cd54dj$1le0$1@digitaldaemon.com...
> > It always reports 4, irrespective of how many members I load into it.
>
> Example, please.

Funny. I thought you were going to say something else. Arf arf..

Here you go:


    template X(T)
    {
        class X
        {
            int i;
            X   next;
            T   t;
        }
    }

    template Y(T)
    {
        class Y
        {
            int     i;
            long    l;
            Y       next;
            T       t;
        }
    }

    int main()
    {
        alias   X!(int) X_int_t;
        alias   Y!(int) Y_int_t;

        X_int_t x   =   new X_int_t;
        Y_int_t y   =   new Y_int_t;

        printf("X_int_t.sizeof: %u\n", x.sizeof);
        printf("Y_int_t.sizeof: %u\n", y.sizeof);
        printf("X_int_t.sizeof: %u\n", X_int_t.sizeof);
        printf("Y_int_t.sizeof: %u\n", Y_int_t.sizeof);

        return 0;
    }


July 17, 2004
Sorry, rushing as usual.

All sizes reported as 4, which seems a little wrong to me.

Let me know if it's wrong, or if I've done something very silly. :)

"Matthew" <admin@stlsoft.dot.dot.dot.dot.org> wrote in message news:cdacjf$us0$3@digitaldaemon.com...
>
> "Walter" <newshound@digitalmars.com> wrote in message news:cda7v0$sd2$3@digitaldaemon.com...
> >
> > "Matthew Wilson" <admin.hat@stlsoft.dot.org> wrote in message news:cd54dj$1le0$1@digitaldaemon.com...
> > > It always reports 4, irrespective of how many members I load into it.
> >
> > Example, please.
>
> Funny. I thought you were going to say something else. Arf arf..
>
> Here you go:
>
>
>     template X(T)
>     {
>         class X
>         {
>             int i;
>             X   next;
>             T   t;
>         }
>     }
>
>     template Y(T)
>     {
>         class Y
>         {
>             int     i;
>             long    l;
>             Y       next;
>             T       t;
>         }
>     }
>
>     int main()
>     {
>         alias   X!(int) X_int_t;
>         alias   Y!(int) Y_int_t;
>
>         X_int_t x   =   new X_int_t;
>         Y_int_t y   =   new Y_int_t;
>
>         printf("X_int_t.sizeof: %u\n", x.sizeof);
>         printf("Y_int_t.sizeof: %u\n", y.sizeof);
>         printf("X_int_t.sizeof: %u\n", X_int_t.sizeof);
>         printf("Y_int_t.sizeof: %u\n", Y_int_t.sizeof);
>
>         return 0;
>     }
>
>


July 17, 2004
Matthew wrote:

> Sorry, rushing as usual.
> 
> All sizes reported as 4, which seems a little wrong to me.
> 
> Let me know if it's wrong, or if I've done something very silly. :)

It looks like all class types report a sizeof of 4.  I assume it's the size of a reference, not the number of bytes occupied by an instance. (it makes sense: we're not supposed to know or care about the internal layout of class types)

    import std.stdio;

    class A {}
        class B{int a,b,c,d,e,f,g,h,i;}
        struct C{int a,b,c,d,e,f,g,h,i;}
    int main() {
        writefln("A:", A.sizeof);
        writefln("B:", B.sizeof);
        writefln("C:", C.sizeof);
        return 0;
    }

yields:

A:4
B:4
C:36

 -- andy
July 17, 2004
Of course, it lends itself to that interpretation. That's why I tried the class.sizeof as well as the instance.sizeof. However, even if D defines the size of an object instance to be the size of an object reference - which I think is a little dodgy in itself - surely a class.sizeof should give the right answer?

"Andy Friesen" <andy@ikagames.com> wrote in message news:cdakdk$11sv$1@digitaldaemon.com...
> Matthew wrote:
>
> > Sorry, rushing as usual.
> >
> > All sizes reported as 4, which seems a little wrong to me.
> >
> > Let me know if it's wrong, or if I've done something very silly. :)
>
> It looks like all class types report a sizeof of 4.  I assume it's the size of a reference, not the number of bytes occupied by an instance. (it makes sense: we're not supposed to know or care about the internal layout of class types)
>
>      import std.stdio;
>
>      class A {}
>          class B{int a,b,c,d,e,f,g,h,i;}
>          struct C{int a,b,c,d,e,f,g,h,i;}
>      int main() {
>          writefln("A:", A.sizeof);
>          writefln("B:", B.sizeof);
>          writefln("C:", C.sizeof);
>          return 0;
>      }
>
> yields:
>
> A:4
> B:4
> C:36
>
>   -- andy


July 17, 2004
"Matthew" <admin@stlsoft.dot.dot.dot.dot.org> wrote in message news:cdaqhg$16ge$1@digitaldaemon.com...
> Of course, it lends itself to that interpretation.

That's exactly what's happening. Classes are reference types, so their sizeof is 4 bytes (will be 8 on AMD64). It must work this way, otherwise .sizeof would be useless for things like walking varargs data.

> That's why I tried the
> class.sizeof as well as the instance.sizeof. However, even if D defines
the size
> of an object instance to be the size of an object reference - which I
think is a
> little dodgy in itself - surely a class.sizeof should give the right
answer?

.classinfo.init.length