On 1/1/23 01:01, Paul wrote:

> ...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?

As matheus said, classes are reference types, meaning, class variables are implemented as pointers. The values above are the addresses of those pointers. The fact that those values are different by 4 tells us that the code was compiled for 32 bits.

On the other hand, matheus's values were 8 apart because that compilation was 64 bits.

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

You must have missed my earlier answer: You need to cast the variable to 'void*'. That special operation will give you the address of the object. I am adding the following last line to matheus's example:

// ...
    writeln("\n",&(MyClassO1.c),"\t",&(MyClassO2.c));
    writeln("\n",cast(void*)MyClassO1,"\t",cast(void*)MyClassO2);

The output:

[...]
7F125FE77010    7F125FE77030  <-- The addresses of the 'c' members

7F125FE77000    7F125FE77020  <-- The addresses of the objects

The reason why objects are 0x10 bytes before the 'c' members is because a class object has two hidden initial members: the vtbl pointer and the monitor, both of which are size of a pointer (4 on your system, and 8 on matheus's system and mine).

Additionally, if you define the class as extern(C++), there will not be the monitor member. The reason for that is C++ does not have that concept and it would break interoperability.

Ali

Reply via email to