Thread: Pass by pointer, reference, value.

  1. #1
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313

    Pass by pointer, reference, value.

    I've been reading the C++ FAQ Lite, and have come across a concept that I think makes sense, but I'm not sure I'm understanding correctly. It's passing by reference, value, and pointer. What I'd like is to explain what I think each means, and then if I'm wrong, perhaps get an explanation why?



    Pointer

    Passing by pointer is where you pass a function a pointer to the object you want worked with. You are not actually changing the object itself, just the local or temporary copy that you have made with the pointer.

    This code is wrong. Both output 10 and I can not for the life of me figure out why that is. Perhaps I am using pointers wrong.

    Code:
    #include <iostream>
    
    int foo(int *bar)
    {
    	*bar = (*bar + *bar);
    	
    	return(*bar);
    }
    
    int main()
    {
    	int baz = 5;
    	int *pbaz = &baz;
    
    	std::cout << foo(pbaz); // Should equal 10
    	std::cout << "\n" << baz; // Should equal 5
    
    	std::cin.get();
    	return(0);
    }


    Reference

    Passing by reference means that you don't pass the value of an object, you are passing the object itself. There are no copies made, only the original object is what is worked with.

    Code:
    #include <iostream>
    
    int foo(int &bar)
    {
    	bar = (bar + bar);
    	
    	return(bar);
    }
    
    int main()
    {
    	int baz = 5;
    
    	std::cout << foo(baz); // Should equal 10
    	std::cout << "\n" << baz; // Should equal 10
    
    	std::cin.get();
    	return(0);
    }


    Value

    Passing by value means that you are taking a copy of what has been passed to the function, work with it in the function, but do not modify the original value.

    Code:
    #include <iostream>
    
    int foo(int bar)
    {
    	bar = (bar + bar);
    	
    	return(bar);
    }
    
    int main()
    {
    	int baz = 5;
    
    	std::cout << foo(baz); // Should equal 10
    	std::cout << "\n" << baz; // Should equal 5
    
    	std::cin.get();
    	return(0);
    }


    Help would be much appreciated with the passing by pointers.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Pointer: A pointer hold the ADDRESS of some object.
    Code:
    int a = 5;
    int *p = &a;
    a = 6;
    cout << *p; // what will be displayed?
    p "points" to a. that means p is an object holding the address (or more low level: simply an integer value representing the address) of some object.
    so you can access the object that is being point to INDIRECTLY via a pointer.
    if the value of a changes, the pointer remains unchanged - it sill points to a.
    thus whenever a changes and you access a indirectly via the pointer you will get the new value of a
    if you change a indirecly via the pointer than a itself changes.

    Call by Reference: a reference is basically a pointer which behaves syntactically as the object itself. that means still no copy of the original object is made - thus you would still change the original object whenever you work with its reference.

    Call by Value: this is the case where a copy of the original object is made. changing the copied object does not affect the original one.


    Output 10 is correct.
    you passed the address of the variable baz (that is a POINTER) to the function.
    the function modified baz indirectly (since you accessed the original baz via a pointer). thus baz is 10 when the function returns.
    Last edited by Raven Arkadon; 02-24-2005 at 02:15 PM.
    signature under construction

  3. #3
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    So when I'm passing by pointer, I'm actually (basically) passing by reference. The original object will be modified - I just passed the function the memory address of the object and am working with that?

    ..So why ever pass by pointer?

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Lithorien
    So when I'm passing by pointer, I'm actually (basically) passing by reference. The original object will be modified - I just passed the function the memory address of the object and am working with that?

    ..So why ever pass by pointer?
    That's right. To me, passing by pointer is most useful when you're passing optional or output parameters because it allows NULL values.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by pianorain
    That's right. To me, passing by pointer is most useful when you're passing optional or output parameters because it allows NULL values.
    Aha. NULL values - That makes sense.



    Thank you both. I think I get the pointers bit better now.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    since c doesnt support references

    imo its a question of taste wheter to use pointers or references.
    what they do is basically the same - references are more high level.
    when using pointers you see immediatly that its a pointer (compiler error if you do something wrong).
    when you have a reference you cant distinguish it from an ordinary object (by syntax)

    [edit]
    loooool... yes and nulls darn... thats why i often use pointers
    signature under construction

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I finally understood passing-by-value and passing-by-reference when I realized pointers are also passed by value, and when I was told that references are actually implemented as constant pointers. So, that means, everything is passed by value:

    1)When you pass an object to a function, a copy is made for the function, so when you change the object inside the function, you are changing a copy, and the original is unaffected. When the function ends, the copy is destroyed.

    2) When you pass a pointer to a function, you are passing an address in memory. Just like with an object, a copy of the pointer is made for the function. And, just like with passing an object, if you try to change the pointer, you only change the copy. The following code demonstrates that:
    Code:
    void func(int* p)
    {
    	int* pi = new int(10);
    	cout<<pi<<endl;
    	p = pi;
    }
    
    int main()
    {
    	int i = 10;
    	int* p = &i;
    	cout<<p<<endl;
    
    	func(p);
    	cout<<p<<endl;
    
    	return 0;
    }
    The output should show that the address remains unchanged. However, if you use the pointer to change the object at that address, then you will make permanent changes to the object. To make changes to an object at a certain address, it doesn't matter whether you have a copy of the address or the original address. Furthermore, when the copy of the address gets destroyed, it doesn't affect the object at that address.

    3) A reference acts like a constant pointer(or is implemented as a constant pointer). So, once again, a copy of the pointer is made for the function. However, unlike the example above, you cannot try to change the address stored in the pointer without getting a compiler error. A constant pointer means that you cannot change the address that is stored in the pointer.
    Code:
    int main()
    {
    	int i = 10;
    	int& r = i;
    	cout<<r<<endl;
    
    	int n = 100;
    	r = &n; //error
    	
    	return 0;
    }
    It is interesting to note that you can change the value at the address stored in the reference. In other words, a reference points to a constant spot in memory, but the value that occupies that spot can change:
    Code:
    int main()
    {
    	int i = 10;
    	int& r = i;
    	cout<<r<<endl;
    
    	int n = 100;
    	r = n;  //ok--changed the value at the address pointed to by r
    	cout<<r<<endl;
    	
    	return 0;
    }
    Note: a constant pointer can be null, but as noted previously a reference cannot be null, so a reference does not behave indentically to a constant pointer.

    See here for a nice illustration:

    http://www.dgp.toronto.edu/~patrick/...ntersVsRef.pdf

    However, his conclusion is wrong:
    Therefore, to summarize, a pointer can point to many different objects during its lifetime, a reference
    can refer to only one object during its lifetime.
    Here is a program where a reference is made to refer to two objects:
    Code:
    class Apple
    {
    public:
    	int num;
    	Apple(int n):num(n){}
    };
    
    
    int main()
    {
    	Apple a(10);
    	Apple& r = a;
    	cout<<r.num<<endl;
    
    	Apple b(50);
    	r = b;
    	cout<<r.num<<endl;
    	
    	return 0;
    }
    Last edited by 7stud; 02-25-2005 at 01:49 AM.

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by 7stud
    Here is a program where a reference is made to refer to two objects:
    You might want to rethink that. Observe the output of the following program:
    Code:
    class Apple
    {
    public:
    	int num;
    	Apple(int n):num(n){}
    	Apple& operator=(const Apple& a)
    		{
    			std::cout << "Inside =operator" << std::endl;
    			num = a.num;
    			return *this;
    		}
    };
    
    
    int main()
    {
    	Apple a(10);
    	Apple b(50);
    	Apple& r = a;
    	std::cout << a.num << " " << r.num << " " << b.num << std::endl;
    
    	r = b;
    	std::cout << a.num << " " << r.num << " " << b.num << std::endl;
    	
    	return 0;
    }
    As you can see, the reference isn't changing. You're actually assign b to a.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You might want to rethink that. Observe the output of the following program:
    That makes sense. The reference is a constant pointer to an area in memory, so you can change what is at that location in memory, but you can't change the address of the pointer.

    Thanks for clearing that up. I guess I lied when I said "I finally understood passing-by-value and passing-by-reference when..." Maybe now I finally understand it.
    Last edited by 7stud; 02-25-2005 at 10:08 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reference to a pointer to a pointer
    By Sharke in forum C++ Programming
    Replies: 18
    Last Post: 05-26-2009, 02:47 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Passing a pointer as a reference
    By hYph3n in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2006, 01:45 PM
  4. C++ pointer vs reference
    By C-Dumbie in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2002, 04:08 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM