Address of a class object

Paul phshaffer at gmail.com
Thu Jan 5 04:04:39 UTC 2023


> (Again, there is no problem here; we are just learning.)
> Ali

Do I have this much right?
```d
import std.stdio, std.traits;
class MyClass {char c;}

void main() {
     auto MyInt = 1;
     writeln("The address of MyInt is      :  ",&MyInt," (stack)");
     auto MyClassVar1 = new MyClass();
     writeln("The address of MyClassVar1 is:  ",&MyClassVar1," 
(stack)");
     auto MyClassVar2 = new MyClass();
     writeln("The address of MyClassVar2 is:  ",&MyClassVar2," 
(stack)");

     writeln;

     auto MyClassObj1 = cast(void*)MyClassVar1;
     writeln("The address of MyClassObj1 is: ",MyClassObj1," 
(heap)");
     auto MyClassObj2 = cast(void*)MyClassVar2;
     writeln("The address of MyClassObj2 is: ",MyClassObj2," 
(heap)");
}
```
..with this output?
```
The address of MyInt is      :  1CA1CFFB1C (stack)
The address of MyClassVar1 is:  1CA1CFFB10 (stack)
The address of MyClassVar2 is:  1CA1CFFB08 (stack)

The address of MyClassObj1 is: 1EB93212000 (heap)
The address of MyClassObj2 is: 1EB93212020 (heap)
```



More information about the Digitalmars-d-learn mailing list