digitalmars.D.learn - size of a class instance
- Bill Baxter (4/4) Nov 12 2006 Is this really the best way to get the size of a class instance given
- Sean Kelly (5/9) Nov 13 2006 Yes. And this only works at run time. I proposed a new .isizeof
- Craig Black (4/13) Nov 13 2006 I like isizeof better than classinfo.init.length. Not only for brevity,...
- Bill Baxter (4/19) Nov 13 2006 Well you've got my vote. MyClass.classinfo.init.length is terribly
- Lionello Lunesu (4/17) Nov 13 2006 I suppose sizeof(MyClass) could simply return the right value. I don't
- Bill Baxter (23/42) Nov 14 2006 I suspect the reason is that for doing various low-level things you
- Sean Kelly (9/14) Nov 14 2006 But the existing method only works at run time, which doesn't allow for
- Bill Baxter (8/24) Nov 14 2006 Oh yeh. Forgot about the runtime only thing. So I'll change my tune
- Lionello Lunesu (4/53) Nov 16 2006 Uh, according to the D spec, the order of the members of a _class_ may
- Bill Baxter (34/46) Nov 16 2006 Tell that to dmd 0.174:
Is this really the best way to get the size of a class instance given the type? MyClass.classinfo.init.length; --bb
Nov 12 2006
Bill Baxter wrote:Is this really the best way to get the size of a class instance given the type? MyClass.classinfo.init.length;Yes. And this only works at run time. I proposed a new .isizeof property to simplify things, but it didn't seem terribly popular. See: http://d.puremagic.com/issues/show_bug.cgi?id=88 Sean
Nov 13 2006
I like isizeof better than classinfo.init.length. Not only for brevity, but a compile time value could be useful. "Sean Kelly" <sean f4.ca> wrote in message news:ej9u8k$2ek2$1 digitaldaemon.com...Bill Baxter wrote:Is this really the best way to get the size of a class instance given the type? MyClass.classinfo.init.length;Yes. And this only works at run time. I proposed a new .isizeof property to simplify things, but it didn't seem terribly popular. See: http://d.puremagic.com/issues/show_bug.cgi?id=88 Sean
Nov 13 2006
Sean Kelly wrote:Bill Baxter wrote:Well you've got my vote. MyClass.classinfo.init.length is terribly unintuitive compared to sizeof(MyClass) in c++. --bbIs this really the best way to get the size of a class instance given the type? MyClass.classinfo.init.length;Yes. And this only works at run time. I proposed a new .isizeof property to simplify things, but it didn't seem terribly popular. See: http://d.puremagic.com/issues/show_bug.cgi?id=88 Sean
Nov 13 2006
Sean Kelly wrote:Bill Baxter wrote:I suppose sizeof(MyClass) could simply return the right value. I don't see how the size of a class' handle can be useful. L.Is this really the best way to get the size of a class instance given the type? MyClass.classinfo.init.length;Yes. And this only works at run time. I proposed a new .isizeof property to simplify things, but it didn't seem terribly popular. See: http://d.puremagic.com/issues/show_bug.cgi?id=88 Sean
Nov 13 2006
Lionello Lunesu wrote:Sean Kelly wrote:I suspect the reason is that for doing various low-level things you might actually need to know how much space the thing is occupying on the stack. Like a varargs, for instance. You need to know how big the thing is on the stack to advance the _argptr properly. It doesn't matter how big the instance data on the heap is. I just now did a web search to see what other GC languages that hide pointers from you do. Java just doesn't have sizeof apparently. "For all other (non predefined types), the result of the sizeof operator is implementation-defined and is classified as a value, not a constant. ... When applied to an operand that has struct type, the result is the total number of bytes in a variable of that type, including any padding." So basically it sounds like they say you can't count on sizeof(aclass) returning anything in particular. I guess when it comes down to it, finding the size of a class is really only useful for satisfying one's curiosity and optimizing member orders to get better alignment. And for that purpose I guess it doesn't really have to be that intuitive a name. --bbBill Baxter wrote:I suppose sizeof(MyClass) could simply return the right value. I don't see how the size of a class' handle can be useful. L.Is this really the best way to get the size of a class instance given the type? MyClass.classinfo.init.length;Yes. And this only works at run time. I proposed a new .isizeof property to simplify things, but it didn't seem terribly popular. See: http://d.puremagic.com/issues/show_bug.cgi?id=88 Sean
Nov 14 2006
Bill Baxter wrote:I guess when it comes down to it, finding the size of a class is really only useful for satisfying one's curiosity and optimizing member orders to get better alignment. And for that purpose I guess it doesn't really have to be that intuitive a name.But the existing method only works at run time, which doesn't allow for things like this: byte[MyClass.isizeof] buf; MyClass c = new(&buf[0]) MyClass(); scope(exit) delete c; Evil, I know, but it kind of stinks that alloca is the only means we currently have to construct classes on the stack. Sean
Nov 14 2006
Sean Kelly wrote:Bill Baxter wrote:Oh yeh. Forgot about the runtime only thing. So I'll change my tune to: if classinfo.init.length were available at runtime *then* it would be good enough. But I presume making that so is not much easier than adding isizeof. If it were easy, then classinfo.init.length wouldn't have had the runtime restriction to begin with. --bbI guess when it comes down to it, finding the size of a class is really only useful for satisfying one's curiosity and optimizing member orders to get better alignment. And for that purpose I guess it doesn't really have to be that intuitive a name.But the existing method only works at run time, which doesn't allow for things like this: byte[MyClass.isizeof] buf; MyClass c = new(&buf[0]) MyClass(); scope(exit) delete c; Evil, I know, but it kind of stinks that alloca is the only means we currently have to construct classes on the stack.
Nov 14 2006
Bill Baxter wrote:Lionello Lunesu wrote:Uh, according to the D spec, the order of the members of a _class_ may be changed by the compiler, so optimizing manually would have no effect. L.Sean Kelly wrote:I suspect the reason is that for doing various low-level things you might actually need to know how much space the thing is occupying on the stack. Like a varargs, for instance. You need to know how big the thing is on the stack to advance the _argptr properly. It doesn't matter how big the instance data on the heap is. I just now did a web search to see what other GC languages that hide pointers from you do. Java just doesn't have sizeof apparently. "For all other (non predefined types), the result of the sizeof operator is implementation-defined and is classified as a value, not a constant. ... When applied to an operand that has struct type, the result is the total number of bytes in a variable of that type, including any padding." So basically it sounds like they say you can't count on sizeof(aclass) returning anything in particular. I guess when it comes down to it, finding the size of a class is really only useful for satisfying one's curiosity and optimizing member orders to get better alignment. And for that purpose I guess it doesn't really have to be that intuitive a name. --bbBill Baxter wrote:I suppose sizeof(MyClass) could simply return the right value. I don't see how the size of a class' handle can be useful. L.Is this really the best way to get the size of a class instance given the type? MyClass.classinfo.init.length;Yes. And this only works at run time. I proposed a new .isizeof property to simplify things, but it didn't seem terribly popular. See: http://d.puremagic.com/issues/show_bug.cgi?id=88 Sean
Nov 16 2006
Lionello Lunesu wrote:Bill Baxter wrote:I guess when it comes down to it, finding the size of a class is really only useful for satisfying one's curiosity and optimizing member orders to get better alignment. And for that purpose I guess it doesn't really have to be that intuitive a name.Lionello Lunesu wrote: Uh, according to the D spec, the order of the members of a _class_ may be changed by the compiler, so optimizing manually would have no effect. L.Tell that to dmd 0.174: import std.stdio : writefln; class A { char a; int b; char c; char d; char e; } class B { char a; char c; char d; char e; int b; } void main() { writefln("Sizeof A = ", A.classinfo.init.length); writefln("Sizeof B = ", B.classinfo.init.length); }And in practice A will probably take 20 bytes, because the thing after it in memory will want to be aligned on a 4-byte boundary too. So the key word in the spec is that the order _may_ be changed, not that it will. C and C++ specs have always had the same sort of language, but in practice very few if any compilers reorder members. They will, however, add in padding to get optimal alignment. Yes, optimizations such as the above are, strictly speaking, implementation-dependent. But that's OK if you've fixed your implementation. --bbSizeof A = 19 Sizeof B = 16
Nov 16 2006