Do static variables in class consume memory of instances?

Mark Lagodych lgd.mrk at gmail.com
Thu Jul 22 16:07:22 UTC 2021


On Thursday, 22 July 2021 at 15:50:59 UTC, Mark Lagodych wrote:
> Do static variables consume *any* memory in instances, perhaps 
> just for pointers to the variables?

Figured it out.

Let's try:
```d
import std.stdio;

class A {
     static int 
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
}

void main() {
     writeln(__traits(classInstanceSize, A));
}
```

Prints: `16`
Without static: `120`
Difference: `104`, exactly `26 * int.sizeof`

So, the answer is **NO**, no memory is taken from instances.


More information about the Digitalmars-d-learn mailing list