Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
January 01, 2006 curious .sizeof object | ||||
---|---|---|---|---|
| ||||
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 |
January 01, 2006 Re: curious .sizeof object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Garett Bass | BTW, I'm using DMD v0.142. |
January 01, 2006 Re: curious .sizeof object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Garett Bass | 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 |
January 01, 2006 Re: curious .sizeof object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | > 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.
|
January 01, 2006 Re: curious .sizeof object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Garett Bass | 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 |
January 02, 2006 Re: curious .sizeof object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Garett Bass | 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
>
>
|
Copyright © 1999-2021 by the D Language Foundation