digitalmars.D.learn - Do static variables in class consume memory of instances?
- Mark Lagodych (6/7) Jul 22 2021 Do static variables consume *any* memory in instances, perhaps
- Adam D Ruppe (3/7) Jul 22 2021 right
- Mark Lagodych (2/9) Jul 22 2021 Thank you! Didn't see your reply.
- Mark Lagodych (17/19) Jul 22 2021 Figured it out.
[D documentation](https://dlang.org/spec/attribute.html#static) says:Static data has one instance per thread, not one per object.Do static variables consume *any* memory in instances, perhaps just for pointers to the variables? Or does compiler automatically convert `someObject.someStaticMember` to `SomeClass.someStaticMember`?
Jul 22 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?nopeOr does compiler automatically convert `someObject.someStaticMember` to `SomeClass.someStaticMember`?right
Jul 22 2021
On Thursday, 22 July 2021 at 16:05:41 UTC, Adam D Ruppe wrote:On Thursday, 22 July 2021 at 15:50:59 UTC, Mark Lagodych wrote:Thank you! Didn't see your reply.Do static variables consume *any* memory in instances, perhaps just for pointers to the variables?nopeOr does compiler automatically convert `someObject.someStaticMember` to `SomeClass.someStaticMember`?right
Jul 22 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.
Jul 22 2021