www.digitalmars.com         C & C++   DMDScript  

D - What does 'this' point to?

reply Matthew <Matthew_member pathlink.com> writes:
I have enough confusion about the pass by reference or is it value conventions
in D that I usually just pass my class or structs as inout parameters to mimic
Java or C like behaviour.  However I am in the midst of debugging a rather
complex tree like data structure that uses a bit of recursion.  I wanted to keep
track of which class reference is pointing to which class.  I thought I would
simply print out the addresses of the class references and compare those with
one another to see.  The surprise I found was that the pointers were changing
all the time.  In fact the 'this' pointer changes when making a call from one
function to another within the same class!  Observe the following code snippet:


// start code
import std.file ;

class Bigclass
{

int var = 0 ;

void ptr1() {
printf("In ptr1 var: %d this: %x\n", var, cast(uint) &this) ;
}

void ptr2() {
var = 2 ;
printf("In ptr2 var: %d this: %x\n", var,cast(uint)  &this) ;
ptr1() ;
}

} // end Bigclass



int main(char[][] args)
{


Bigclass cl = new Bigclass() ;
cl.ptr2() ;
return(0) ;

}
// finish code

The output is:
In ptr2 var: 2 this: 12ff2c
In ptr1 var: 2 this: 12ff10


Thus the address of the this  reference has actually changed from one method
call to another within the same class!  This is going to make debugging pretty
tricky I think.  Any comments on this behavior?
Apr 05 2005
next sibling parent reply Benjamin Herr <ben 0x539.de> writes:
Matthew wrote:
 In fact the 'this' pointer changes when making a call from one
 function to another within the same class!  Observe the following code snippet:

 printf("In ptr1 var: %d this: %x\n", var, cast(uint) &this) ;

You are not printing the value of the this-ptr. You are printing its location on the stack. #class Foo { # void p() { # printf("%p\n", &this); # } #} # # #int main() { # Foo f = new Foo; # f.p(); # f.p(); // equal # # delegate { # f.p(); // different # }(); # # return 0; #} printf("%p\n", this) should do what you want. Object references seem to be actually pointers. --Benjamin
Apr 05 2005
parent Matthew <Matthew_member pathlink.com> writes:
In article <d2ufpj$2dhd$1 digitaldaemon.com>, Benjamin Herr says...
Matthew wrote:
 In fact the 'this' pointer changes when making a call from one
 function to another within the same class!  Observe the following code snippet:

 printf("In ptr1 var: %d this: %x\n", var, cast(uint) &this) ;

You are not printing the value of the this-ptr. You are printing its location on the stack. #class Foo { # void p() { # printf("%p\n", &this); # } #} # # #int main() { # Foo f = new Foo; # f.p(); # f.p(); // equal # # delegate { # f.p(); // different # }(); # # return 0; #} printf("%p\n", this) should do what you want. Object references seem to be actually pointers. --Benjamin

Thanks Benjamin that seems to help. I did realize that class objects had to be pointer references, otherwise the performance of some recursive data structures such as trees or graphs would be abysmal, when in fact I get very good performance, even better than C in some cases. Regards
Apr 05 2005
prev sibling parent "Walter" <newshound digitalmars.com> writes:
"Matthew" <Matthew_member pathlink.com> wrote in message
news:d2ucb2$2966$1 digitaldaemon.com...
 I have enough confusion about the pass by reference or is it value

 Thus the address of the this  reference has actually changed from one

 call to another within the same class!  This is going to make debugging

 tricky I think.  Any comments on this behavior?

You're printing the address of 'this', not the value of 'this'. 'this' is implemented as a local variable in each method. 'this' is implemented as a pointer to the instance, as it is in C++. Try your program in C++, you'll find the results are the same.
Apr 15 2005