FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Reference and Pointer

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
sofmz



Joined: 08 Jul 2004
Posts: 2

PostPosted: Thu Jul 08, 2004 8:46 pm    Post subject: Reference and Pointer Reply with quote

I'm absolute new to D programming. The following is the code i have written for testing purpose.

int main ()
{
Computer c1 = new Computer();
Computer c2 = c1;
Computer* c3 = &c1;

printf("c1 : ?d\n", &c1);
printf("c2 : ?d\n", &c2);
printf("c3 : ?d\n", c3);

return 0;
}

//It prints :-

c1 : 1244972
c2 : 1244976
c3 : 1244972

Before it, i have tried to change the value of an c2's variable and found that the same variable of c1 is changed as well. So i assumed that c2 is a reference of c1 and the result of c2 should be same with c1 and c3, but it's not. May anyone kindly let me know the reason??
Back to top
View user's profile Send private message Send e-mail
Carlos



Joined: 19 Mar 2004
Posts: 396
Location: Canyon, TX

PostPosted: Thu Jul 08, 2004 9:11 pm    Post subject: Reply with quote

c1 and c2 are references. That is, in addresses 1244972 and 1244976 there's the same value: the address of the actual object. c3 is a pointer, so its content is the address of c1.
Something like this:

var / type / address / content
c1 / Computer-ref / 1244972 / xx
c2 / Computer-ref / 1244976 / xx
c3 / Computer-ref * / yy / 1244972
(anonymous) / Computer / xx / (the object)

At least that's what I understand. Somebody correct me if I'm wrong.
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
JJR



Joined: 22 Feb 2004
Posts: 1104

PostPosted: Fri Jul 09, 2004 1:16 am    Post subject: Reply with quote

Just confirming what Carlos posted...

As Carlos pointed out, the pointer c3 is a variable that stores the address of c1. If you were to do &c3 as you did with c1 and c2 in the printf, you would see that the actual address of c3 is completely original also (in fact it's probably 1244980).

In the printf you asked for c3's stored value. On the other hand, for c1 and c2, you asked for the actual addresses of those variables (&c1 and &c2), NOT there contents. c1 and c2 are DIFFERENT variables that contain the SAME content (ie a reference/address to the newly allocated Computer() object).

Thus these are synonymous:

c1, c2, *c3 --> all point to the new Computer object.

&c1, c3 --> points to address of c1

These are different:

&c1, &c2, &c3 --> all different addresses because they are different variables and represent different slots in memory for storing values.

Hope that helps,

Later,

John
Back to top
View user's profile Send private message
JJR



Joined: 22 Feb 2004
Posts: 1104

PostPosted: Fri Jul 09, 2004 1:47 am    Post subject: Re: Reference and Pointer Reply with quote

sofmz wrote:
Before it, i have tried to change the value of an c2's variable and found that the same variable of c1 is changed as well. So i assumed that c2 is a reference of c1 and the result of c2 should be same with c1 and c3, but it's not. May anyone kindly let me know the reason??


I was just reading over your post again and realized that their might be another facet to your question that was unanswered in the previous posts.

When you say that you found that the same variable c1 was changed when you changed the contents of c2, what did you actually do? c1 should not change when you change the value of c2; if that happened, it would indeed make things very confusing! c1 and c2 are just variables and are not references to each other (they just reference the same object).

Here's an example just to clarify that we're talking about the same thing:

Code:
class Computer {}

int main()
{
    Computer c1 = new Computer;
    Computer c2 = c1;

    printf("\n?d = c1", c1);
    printf("\n?d = c2", c2);

    // now change c2 -- contents of c1 do not change!
    c2 = new Computer;

    printf("\n?d = c1", c1);
    printf("\n?d = c2", c2);

    return 0;
}
Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Fri Jul 09, 2004 3:19 am    Post subject: Re: Reference and Pointer Reply with quote

JJR wrote:
When you say that you found that the same variable c1 was changed when you changed the contents of c2, what did you actually do? c1 should not change when you change the value of c2; if that happened, it would indeed make things very confusing! c1 and c2 are just variables and are not references to each other (they just reference the same object).

I re-read it a couple of times, and I think he meant that he changed the value of a field/member, not the value of c2 itself. In which case, the behavior he got is definitely expected.
_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
nail



Joined: 30 Jun 2004
Posts: 18
Location: Krasnoznamensk, Moscow region, Russia

PostPosted: Fri Jul 09, 2004 4:59 am    Post subject: Reply with quote

Ahem.... What I should to do if I want to copy contents of c1 to c2 ?
Back to top
View user's profile Send private message
Carlos



Joined: 19 Mar 2004
Posts: 396
Location: Canyon, TX

PostPosted: Fri Jul 09, 2004 11:31 am    Post subject: Reply with quote

nail wrote:
Ahem.... What I should to do if I want to copy contents of c1 to c2 ?

You would have to define such a function. The convention (not written, but assumed) is to use "dup" for that.
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
JJR



Joined: 22 Feb 2004
Posts: 1104

PostPosted: Fri Jul 09, 2004 5:03 pm    Post subject: Re: Reference and Pointer Reply with quote

csauls wrote:
JJR wrote:
When you say that you found that the same variable c1 was changed when you changed the contents of c2, what did you actually do? c1 should not change when you change the value of c2; if that happened, it would indeed make things very confusing! c1 and c2 are just variables and are not references to each other (they just reference the same object).

I re-read it a couple of times, and I think he meant that he changed the value of a field/member, not the value of c2 itself. In which case, the behavior he got is definitely expected.


If that is what he meant, then, yes, I agree with you. Changing the value of a member variable in c1 or c2 amounts to changing the same value within the new computer object (since they both reference this object).
Back to top
View user's profile Send private message
sofmz



Joined: 08 Jul 2004
Posts: 2

PostPosted: Mon Jul 12, 2004 8:00 pm    Post subject: Re: Reference and Pointer Reply with quote

JJR wrote:
If that is what he meant, then, yes, I agree with you. Changing the value of a member variable in c1 or c2 amounts to changing the same value within the new computer object (since they both reference this object).


Yes, that's what I meant. Thanks so much for everyone who spent time for helping me, now I understand. Pointer is very useful in C/C++, but in D, since all variables of object are "implicit pointers" , what's the advantage of using pointer in D except able to point to null??

I still have question related to D's reference and pointer, I've modified the previous code :-

Code:
//===========Computer.d============
class Computer
{
   int number;

   this(int num=0)
   {
      number = num;
   }
   
   ~this()
   {
      printf("It's deleted\n");
   }
   
}
//=============================


//=============main.d============
import std.c.stdio;
import Computer;

int main ()
{
   Computer c1 = new Computer(100);
   Computer c2 = c1;
   Computer* c3 = &c1;
   
   delete c2;
   
   printf("c1 : ?d\n", c1.number);
   printf("c3 : ?d\n\n", c3.number);
   
   return 0;
}
//==============================


It prints :-

It's deleted
c1 : 100
c3 : 100


the behavior is out of my expectation! c1, c2 and *c3 points to the same Computer object, after the object is deleted, I expected it should print error message of access violation because the object is supposed no longer exist. I can't figure out why it can access it.
Back to top
View user's profile Send private message Send e-mail
kinghajj



Joined: 13 Jul 2004
Posts: 3

PostPosted: Tue Jul 13, 2004 8:47 pm    Post subject: ... Reply with quote

Don't use pointers unless you REALLY need to. From what I've read, pointers are in D mostly for interfacing with C libraries.

In your example, c1 and c2 have the same value, though are in a different place in memory. So, if you edit c1, c2 will not change. However, c3 is a "pointer" to c1, so if you edit c3, you are actually editing c1.

Here's an example...
Code:

void change_number(int *i)
{
    i = 2;
}

int main()
{
    int i;
    change_number(i);

    printf("?i", i); // will print 2
    return 0;
}


However, I'd recommend this method because it's easier to understand:
Code:

int change_number()
{
    return 2;
}

int main()
{
    int i = change_number();

    printf("?i", i); // will print 2
    return 0;
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group