Address of a class object

Paul phshaffer at gmail.com
Sun Jan 1 09:01:24 UTC 2023


Thanks all. Yes it seems my understanding and "D" vocabulary are 
still a bit confused.

So I'm taking a D course online and was trying to verify what I 
was learning. The course example printed out the size and 
alignment of types...something close to this:
```d
import std.stdio;
import std.traits;

class MyClass {char c;}

void main() {
     writeln(" Size  Alignment  Type\n",
             "=========================");

         size_t size = __traits(classInstanceSize, MyClass);
         size_t alignment = classInstanceAlignment!MyClass;

     writefln("%4s%8s      %s",size, alignment, MyClass.stringof);

	// my test code added
	MyClass MyClassO1;
	MyClass	MyClassO2;
	writeln("\n",&MyClassO1,"\t",&MyClassO2);
}
```
...on my laptop it prints...
```
  Size  Alignment  Type
=========================
    9       4      MyClass

4FFB20  4FFB24
```

If the size of MyClass is 9 bytes why do MyClassO1 & O2 addresses 
only differ by 4 bytes?

Because those addresses(4FFB20  4FFB24) are the addresses of the 
class **variables**, not the addresses of the **objects** 
themselves?

So, I guess my question is actually how do I print out the 
addresses of the MyClassO1 & O2 objects themselves?
```


More information about the Digitalmars-d-learn mailing list