开发者

pointers vs. references vs. a regular pass by value c++

Please read before answering. I dont want you to state the obvious for me. THANKS :D

I am trying to differentiate between pointers and passing by value. And I think I understand them but one thing the source I was reading wasnt clear on is what is the difference between passing a pointer and passing by value. consider the following...

andy = 25;
fred = andy;
ted = &andy; 

The first line passes the value 25 to andy. The second line passes the value of andy to fred. Now fred possess the value 25. the third line passes reference to te开发者_C百科d. Now ted holds the memory address of andy, which holds the value of 25. Now take a look at the next code

andy = 25;
fred = andy;
ted = &andy; 
joe = *andy;

The above code looks just like the first snippet except we pass the pointer of andy to joe. Now joe holds the pointer value of 25. Which almost seems the same as just doing (joe = andy). without *

what's the difference in passing the pointer value compared to just passing by value ???

In my interpretation, It appears that the pointer value of joe is still affected if later down the line I decided to change the value of andy/ compared to if I just passed by value, it not be affect. T

To my knowledge, the only difference between passing by reference and passing by pointer is that one holds the address, and the other holds the value within that address. which can still change if any varible that holds that value reference, decides to change it. If this is the case, what is the significance of passing by reference and passing by value.

Here is the link to what I was reading

http://www.cplusplus.com/doc/tutorial/pointers/

Thanks guys!!!


What you're talking about has nothing to do with pass-by-reference or pass-by-value. Those terms only refer to what happens with parameters when function calls are made. The canonical example is the swap() function. This function:

void swap(int a, int b)
{
    int t = a;
    a = b;
    b = tmp;
}

doesn't do anything; the parameters won't be swapped if you call it, since they're passed by value. If you make them pass-by-reference (in C++) or you pass pointers (in C or C++), it will do what you want it to:

void swap(int *a, int *b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

or in C++ only,

void swap(int &a, int &b)
{
    int t = a;
    a = b;
    b = a;
}

The advantage of the pass-by-reference version is that it's guaranteed to get good references. The first example, if you pass bad pointers, will crash your program.


You can't do joe = *andy because andy isn't a pointer, it's an int.

However, you could do joe = *ted, because ted is a pointer.

Suppose you did this:

andy = 25; 
fred = andy; 
ted = &andy;  
joe = *ted;
andy = 26;
jim = *ted;

Then joe will have a value of 25, while jim will have a value of 26.

The reason for this is that ted is a pointer to andy. Using *ted gives you the value at that pointer, at that time.


I think you're just about there, but your confusing the terminology. Let look at your examples again:

andy = 25;
fred = andy;
ted = &andy;

The first line assigns the value 25 to andy. The second line assigns the value held by andy to fred (25 in this case). The third line assigns the address of andy to ted.

All three variables hold independent values. Changing any one of them will not affect the others.

Now, looking at your next example:

andy = 25;
fred = andy;
ted = &andy;
joe = *andy;

Like you said, the first three lines are the same. The fourth line assigns the value pointed to by andy to joe. In this case joe would now hold the value at memory location 25 because andy is being treated as a memory location (and the actual value is unknown in this case because we don't know from the data given what is stored at address 25).

And, again, all four variable hold independent values at this point. Changing any of them will not affect the value of the others.

So lets look at another example:

andy = 35;
jim = &andy; alex = *jim;

The first line assigns the value of 25 to andy. Nothing new here. The second line assigns the address of andy to jim. The third line dereferences the address stored in jim and assigns the value to alex. The variable alex now holds the value 25. But the 25 that alex holds is a different 25 than what alan holds because alan and alex live at different memory addresses. If we change the value of alan to 30, alex still hold 25.

A final example:

andy = 25;
jim = &andy;
*jim = 30;

The first two lines are just like the previous example, andy has a value of 25 and jim contains the address of andy.

The third line assigns a value of 30 to the memory address pointed to by jim. Since this is the same address that andy lives at, andy now hold a value of 30. In this case andy and *jim reference the same memory location.

The terms pass-by-value and pass-by-pointer (or pass-by-reference) refer to passing parameters to functions. When passing-by-value, as in:

void myfunc(int a) { a = a+5; }

And then called as in:

int b=2;
myfunc(b);

The value of b, upon return from the function is not changed because you merely passed the value of b into the function, assigning it to a. The variable a was changed but since it lives at a different memory address, changing a did not affect the value of b.

But when passing-by-pointer, as in:

void myfunc(int* a) { *a = *a+5; }

And then called as in:

int b=2;
myfunc(&b);

The value of b is changed from 2 to 7 because you passed the address of b to the function and then dereferenced the address to getit's current value, added 5 to it, and stored the result back in to the same address. When the function returns, the value stored at the address of b has been updated.


Your example shows example of assignment and referencing/dereferencing pointer/object, not passing by value or reference. And like what Saxon said above, you cant assign joe=*andy.

In simple you can define them as:

  • passing by value: you pass a copy of the value to a function. The copy existed within the scope of the function only. Any changes to it is local to the scope eg. void funcA(int a)
  • passing by reference: you pass an address of the object that hold the value to the function and the object still exist within its parent scope. You have the address of the object that is in your parent scope so changes will be made to that same object. eg. void funA(int& a)
  • passing by pointer: you pass a pointer that hold the address to that object. You are actually passing the pointer as a value (hold the address to the object) into the method. If you change the pointer to point to another object, the original object in the parent scope doesnt change. If you want to change the object it points to, then you have to deference it. eg. void funA(int* a)


What is the significance of passing by reference and passing by value?

You're right, in this context there is no difference between

(a) assigning the values directly, eg (joe = andy) and
(b) Copying the memory address and then de-referencing eg (ted = &andy; joe = *ted;)

There is no difference because they have the same scope, i.e. they're in the same function. The significance of passing pointers only kicks in once you start passing them to other functions.

When I was trying to understand pointers (just 6 months ago), the main eureka moment for me was (and no-one ever tells you this)...in C, functions pass their parameters by copying them. This is significant because it means that if you just pass variables to other functions, changes to those variables won't be reflected in the other functions in your program.

Lets say you have a program that tracks the number of students in a class. When a new student joins the class, a function AddStudent is called to increment the number of students in the class. There are 2 versions of AddStudent here, pass by reference and pass by value. We'll call each from main, and printf the results to see the difference.

int AddStudent_pass_by_value(int value)
{
    value = value+1;
    printf("Number of students (within function): %d\n", value);
    return 0;
}

int AddStudent_pass_by_reference(int* value)
{
    *value = *value+1;
    printf("Number of students (within function): %d\n", *value);
    return 0;
}

int main()
{
    int NumStudents = 0;  //number of students
    printf("Number of students, (within main): %d\n", NumStudents);
    printf("Pass it by value\n");
    AddStudent_pass_by_value(NumStudents);
    printf("Number of students (within main): %d\n\n", NumStudents); 
    /*In main, the value is still 0! The function has not changed the original variable, just a copy of it.*/

    printf("Now pass it by reference\n");
    AddStudent_pass_by_reference(&NumStudents);
    printf("Number of students (within main): %d\n", NumStudents);  
    /*Now, because we passed by reference, the original variable has been changed. */
    return 0;
}

Hope this helps...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜