digitalmars.D - curious .sizeof object
- Garett Bass (20/20) Jan 01 2006 I discovered a surprising result (to me) when calling sizeof. Here is a...
- Garett Bass (1/1) Jan 01 2006 BTW, I'm using DMD v0.142.
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (14/17) Jan 01 2006 If you really want to know the size, you can use something like:
- Garett Bass (9/16) Jan 01 2006 Shouldn't Foo.sizeof return 20 in this case?
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (8/12) Jan 01 2006 Probably... (would make more sense than returning pointer size)
- Chris Sauls (7/38) Jan 02 2006 Try this, if I remember right:
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
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
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
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=48I 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
Try this, if I remember right: 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