www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - curious .sizeof object

reply "Garett Bass" <garettbass studiotekne.com> writes:
I discovered a surprising result (to me) when calling sizeof.  Here is an 
example:

------------
module main;
private import std.stdio;

class Foo{
    int i = 1, j = 2, k = 3;
}


int main(char[][] args) {
    writefln("Foo.sizeof = %d", Foo.sizeof); // = 4

     Foo foo = new Foo;
    writefln("foo.sizeof = %d", foo.sizeof); // = 4

    return 0;
}

------------

Certainly foo is larger than 4 bytes.  It appears that both Foo.sizeof and 
foo.sizeof return the size of a Foo reference.  So, how do I get the sizeof 
an actual Foo object?

Thanks,
Garett 
Jan 01 2006
next sibling parent "Garett Bass" <garettbass studiotekne.com> writes:
BTW, I'm using DMD v0.142. 
Jan 01 2006
prev sibling next sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Garett Bass wrote:

 Certainly foo is larger than 4 bytes.  It appears that both Foo.sizeof and 
 foo.sizeof return the size of a Foo reference.  So, how do I get the sizeof 
 an actual Foo object?

If you really want to know the size, you can use something like: class Foo{ new(size_t size) { printf("%ld\n",size); return new void[size]; } delete(void *p) { delete p; } int i = 1, j = 2, k = 3; } I got 20*. But if you want that kind of control, use a struct... ? struct Bar{ int i = 1, j = 2, k = 3; } // Bar.sizeof = 12 --anders * seems to be about PAR: http://www.digitalmars.com/d/abi.html
Jan 01 2006
parent reply "Garett Bass" <garettbass studiotekne.com> writes:
 If you really want to know the size, you can use something like:

 class Foo{
     new(size_t size) { printf("%ld\n",size); return new void[size]; }
     delete(void *p) { delete p; }

     int i = 1, j = 2, k = 3;
 }

 I got 20*. But if you want that kind of control, use a struct... ?

Shouldn't Foo.sizeof return 20 in this case? I can't use a struct because I want to create a class allocator to allow me to place objects in a pre-allocated pool. I need to know the size of the object so I can create a pool large enough to contain the object. I was originally thinking of creating something more along the lines of C++ placement new. However, I suppose I could handle this by passing a Pool object to the allocator, which could then correctly place the object, and as a bonus, it could throw an exception if the Pool was unable to accomodate the object.
Jan 01 2006
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Garett Bass wrote:

 Shouldn't Foo.sizeof return 20 in this case?

Probably... (would make more sense than returning pointer size) Think it has been suggested before: http://all-technology.com/eigenpolls/dwishlist/index.php?it=40 http://all-technology.com/eigenpolls/dwishlist/index.php?it=48
 I can't use a struct because I want to create a class allocator to allow me 
 to place objects in a pre-allocated pool.  I need to know the size of the 
 object so I can create a pool large enough to contain the object.

You could store size from the allocator, but maybe that's too late... (i.e. the "new" does get passed the size that it needs to allocate) --anders
Jan 01 2006
prev sibling parent Chris Sauls <ibisbasenji gmail.com> writes:
Try this, if I remember right:

# writefln("sizeof Foo = %d", Foo.classinfo.init.sizeof);

What you get with (class).sizeof is the size of an object variable -- which are
almost 
strictly referances, and therefore always 4 bytes on a 32 bit platform.  Its
not 
neccessarily intuitive, I admit.

-- Chris Sauls

Garett Bass wrote:
 I discovered a surprising result (to me) when calling sizeof.  Here is an 
 example:
 
 ------------
 module main;
 private import std.stdio;
 
 class Foo{
     int i = 1, j = 2, k = 3;
 }
 
 
 int main(char[][] args) {
     writefln("Foo.sizeof = %d", Foo.sizeof); // = 4
 
      Foo foo = new Foo;
     writefln("foo.sizeof = %d", foo.sizeof); // = 4
 
     return 0;
 }
 
 ------------
 
 Certainly foo is larger than 4 bytes.  It appears that both Foo.sizeof and 
 foo.sizeof return the size of a Foo reference.  So, how do I get the sizeof 
 an actual Foo object?
 
 Thanks,
 Garett 
 
 

Jan 02 2006