Address of a class object

Paul phshaffer at gmail.com
Wed Jan 4 04:01:09 UTC 2023


matheus, using dmd64 on my laptop to compile and run this:
```d
import std.stdio, std.traits;

class MyClass {char[16] 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
     auto MyClassO1 = new MyClass();
     auto MyClassO2 = new MyClass();
     writeln("\nMyClassObj1     MyClassObj2");
     writeln(cast(void*)MyClassO1,"\t",cast(void*)MyClassO2);
}
```
I get this:
```
  Size  Alignment  Type
=========================
   32       8      MyClass

MyClassObj1     MyClassObj2
21EF8E22000     21EF8E22020
```
If I change this line to:
```
class MyClass {char[1] c;}
```
I get this:
```
  Size  Alignment  Type
=========================
   17       8      MyClass

MyClassObj1     MyClassObj2
27727202000     27727202020
```
If my size is 17 bytes and my alignment is 8 bytes, shouldn't my 
MyClassObj2 in this example be @ 277272020**18** ?


More information about the Digitalmars-d-learn mailing list