Re: Address of a class object

2023-01-31 Thread Paul via Digitalmars-d-learn
On Thursday, 5 January 2023 at 05:59:26 UTC, Ali Çehreli wrote: On 1/4/23 20:04, Paul wrote: >> (Again, there is no problem here; we are just learning.) >> Ali > > Do I have this much right? > ..with this output? Looks good to me. While we're here, you can force the class objects to be on the

Re: Address of a class object

2023-01-05 Thread areYouSureAboutThat via Digitalmars-d-learn
On Thursday, 5 January 2023 at 17:23:39 UTC, H. S. Teoh wrote: On Thu, Jan 05, 2023 at 06:32:47AM +, areYouSureAboutThat Also, I cannot read hex, [...] IMNSHO, anyone who claims to be a programmer should at least know that much. ?? Well, like all, I learnt this at uni. .. as well as

Re: Address of a class object

2023-01-05 Thread Paul via Digitalmars-d-learn
On Thursday, 5 January 2023 at 05:59:26 UTC, Ali Çehreli wrote: While we're here, you can force the class objects to be on the stack as well: scope MyClassVar1 = new MyClass(); I replaced 'auto' with 'scope'. Ali Very interesting. Thanks Ali.

Re: Address of a class object

2023-01-05 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jan 05, 2023 at 06:32:47AM +, areYouSureAboutThat via Digitalmars-d-learn wrote: [...] > Second, to be sure your getting the correct results, it would be nice > if there was a 'category of type' in std.traits for: > > isAllocatedOnStack > isAllocatedOnHeap > > As it is, your just

Re: Address of a class object

2023-01-04 Thread areYouSureAboutThat via Digitalmars-d-learn
On Thursday, 5 January 2023 at 04:04:39 UTC, Paul wrote: .. Do I have this much right? ... First, i would say, add @safe to your main. @safe void main() ... Then you will see you are treading on dangerous waters ;-) Second, to be sure your getting the correct results, it would be nice if

Re: Address of a class object

2023-01-04 Thread Ali Çehreli via Digitalmars-d-learn
On 1/4/23 20:04, Paul wrote: >> (Again, there is no problem here; we are just learning.) >> Ali > > Do I have this much right? > ..with this output? Looks good to me. While we're here, you can force the class objects to be on the stack as well: scope MyClassVar1 = new MyClass(); I

Re: Address of a class object

2023-01-04 Thread Paul via Digitalmars-d-learn
(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 : ",," (stack)"); auto MyClassVar1 = new MyClass();

Re: Address of a class object

2023-01-04 Thread Ali Çehreli via Digitalmars-d-learn
On 1/4/23 13:43, H. S. Teoh wrote: > You do realize that the compiler is free to reorder local variables on > the stack, right? ;-) Of course. :) I was trying different strategies to catch the compiler (dmd here) in a single act of 8-byte object alignment as reported by .alignof. Another

Re: Address of a class object

2023-01-04 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Jan 04, 2023 at 01:20:12PM -0800, Ali Çehreli via Digitalmars-d-learn wrote: > On 1/4/23 12:02, Steven Schveighoffer wrote: > > On 1/4/23 2:27 PM, Ali Çehreli wrote: > > >> I put the objects into a 2-length > >> static array but the difference was still 0x20. (?) > > > > Are you putting

Re: Address of a class object

2023-01-04 Thread Ali Çehreli via Digitalmars-d-learn
On 1/4/23 12:02, Steven Schveighoffer wrote: > On 1/4/23 2:27 PM, Ali Çehreli wrote: >> I put the objects into a 2-length >> static array but the difference was still 0x20. (?) > > Are you putting the class *references* in a 2-length static array? I lied. As I could not put the objects in a

Re: Address of a class object

2023-01-04 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/4/23 2:27 PM, Ali Çehreli wrote: On 1/4/23 10:48, H. S. Teoh wrote: > Allocations are not necessarily consecutive; the GC may have its own > strategy of allocation that doesn't follow a linear sequence. That was one of my guesses. So, I put the objects into a 2-length static array but

Re: Address of a class object

2023-01-04 Thread Ali Çehreli via Digitalmars-d-learn
On 1/4/23 11:27, Ali Çehreli wrote: > writeln("hidden 0: ", hiddenValue(addr, 0)); > writeln("hidden 1: ", hiddenValue(addr, 1)); Silly me! :) Those members have names: writeln("__vptr : ", obj.__vptr); writeln("__monitor : ", obj.__monitor);

Re: Address of a class object

2023-01-04 Thread Ali Çehreli via Digitalmars-d-learn
On 1/4/23 10:48, H. S. Teoh wrote: > Allocations are not necessarily consecutive; the GC may have its own > strategy of allocation that doesn't follow a linear sequence. That was one of my guesses. So, I put the objects into a 2-length static array but the difference was still 0x20. (?) >

Re: Address of a class object

2023-01-04 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Jan 04, 2023 at 09:51:05AM -0800, Ali Çehreli via Digitalmars-d-learn wrote: > On 1/3/23 20:01, Paul wrote: > > > Size Alignment Type > > = > >17 8 MyClass > > > > MyClassObj1 MyClassObj2 > > 27727202000 27727202020 > > ``` > > If my

Re: Address of a class object

2023-01-04 Thread Ali Çehreli via Digitalmars-d-learn
On 1/3/23 20:01, Paul wrote: > 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 @

Re: Address of a class object

2023-01-03 Thread Paul via Digitalmars-d-learn
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

Re: Address of a class object

2023-01-02 Thread Paul via Digitalmars-d-learn
Thank you, Teoh, Ali, & Matheus

Re: Address of a class object

2023-01-01 Thread Ali Çehreli via Digitalmars-d-learn
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

Re: Address of a class object

2023-01-01 Thread matheus via Digitalmars-d-learn
On Sunday, 1 January 2023 at 09:01:24 UTC, Paul wrote: ... 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?

Re: Address of a class object

2023-01-01 Thread Paul via Digitalmars-d-learn
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

Re: Address of a class object

2022-12-31 Thread Ali Çehreli via Digitalmars-d-learn
On 12/31/22 16:35, Paul wrote: > Can I acquire the address of a class object, Answering that question literally, yes, you can by casting the class variable to void*. But note: 'class object' means the instance of class in D. > not a class variable (i.e. > the instantiations of the

Re: Address of a class object

2022-12-31 Thread H. S. Teoh via Digitalmars-d-learn
On Sun, Jan 01, 2023 at 12:35:40AM +, Paul via Digitalmars-d-learn wrote: > Hello. Thanks for any assistance. > > Can I acquire the address of a class object, not a class variable > (i.e. the instantiations of the class) but the object definition > itself? > > ```d &g

Address of a class object

2022-12-31 Thread Paul via Digitalmars-d-learn
Hello. Thanks for any assistance. Can I acquire the address of a class object, not a class variable (i.e. the instantiations of the class) but the object definition itself? ```d class MyClass {char c} ... MyClass MyClassVar; writeln(); // this compiles writeln();// this does not ```

Getting the address of the class object

2010-01-11 Thread Ali Çehreli
Coming from C++, I wonder whether it is ever needed to get the address of the object? If so, how? The operator below produces the address of the class reference (variable). How about the class object? class C {} void main() { auto variable = new C; auto address = variable; } Thank

Re: Getting the address of the class object

2010-01-11 Thread bearophile
Ali Çehreli Wrote: Coming from C++, I wonder whether it is ever needed to get the address of the object? If so, how? I've never had to do this in D. I think you can just cast the class reference to void* and then to the pointer you want, something like (untested): Foo f = new Foo; auto p =

Re: Getting the address of the class object

2010-01-11 Thread Ali Çehreli
bearophile wrote: Ali Çehreli Wrote: Coming from C++, I wonder whether it is ever needed to get the address of the object? If so, how? I've never had to do this in D. I think you can just cast the class reference to void* and then to the pointer you want, something like (untested): Foo f =