www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Getting memory size of class

reply "js.mdnq" <js_adddot+mdng gmail.com> writes:
sizeof always returns 4 or 8 regardless of size of class:

class myclass(T)
{
public:
    T v1;
    T v2;
    T v3;
    T v4;
    T v5;
    T v6;
}

writeln((myclass!byte).sizeof, (myclass!double).sizeof);

or even

writeln((myclass!int).classinfo.init.sizeof, 
(myclass!double).classinfo.init.sizeof);

from

http://forum.dlang.org/thread/dp9hct$nuf$1 digitaldaemon.com

So how does one get the actual memory usage of a class?
Dec 05 2012
next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On 2012-12-05, 20:03, js.mdnq wrote:

 sizeof always returns 4 or 8 regardless of size of class:

 class myclass(T)
 {
 public:
     T v1;
     T v2;
     T v3;
     T v4;
     T v5;
     T v6;
 }

 writeln((myclass!byte).sizeof, (myclass!double).sizeof);

 or even

 writeln((myclass!int).classinfo.init.sizeof,  
 (myclass!double).classinfo.init.sizeof);

 from

 http://forum.dlang.org/thread/dp9hct$nuf$1 digitaldaemon.com

 So how does one get the actual memory usage of a class?

__traits( classInstanceSize, myclass!int ) http://dlang.org/traits.html -- Simen
Dec 05 2012
prev sibling parent Mike Wey <mike-wey example.com> writes:
On 12/05/2012 08:03 PM, js.mdnq wrote:
 sizeof always returns 4 or 8 regardless of size of class:

 class myclass(T)
 {
 public:
     T v1;
     T v2;
     T v3;
     T v4;
     T v5;
     T v6;
 }

 writeln((myclass!byte).sizeof, (myclass!double).sizeof);

 or even

 writeln((myclass!int).classinfo.init.sizeof,
 (myclass!double).classinfo.init.sizeof);

 from

 http://forum.dlang.org/thread/dp9hct$nuf$1 digitaldaemon.com

 So how does one get the actual memory usage of a class?

(myclass!int).classinfo.init.length At least that is what druntime is using. -- Mike Wey
Dec 05 2012