Pointers and Functions

What is the difference between passing a pointer by value and by reference? I'm thinking that either way, the formal paramater will recieve the address of the actual paramater, thus pointing to the same location in each case, value or reference. (Now any changes by the formal parameter will affect the actual paramater).I just started studying pointers so any helpful insights will be appreciated.
I'm thinking that either way, the formal paramater will recieve the address of the actual paramater, thus pointing to the same location in each case, value or reference.

Yes, using either method, you can access the pointer (and change the data pointed to) just fine.

However, if you pass a pointer by value, then you cannot change the pointer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void resizeByValue(int* ptr, int newSize)
{
     delete [] ptr;
     ptr = new int[newSize]; //does not work as expected (only changing a copy)
}

void resizeByReference(int*& ptr, int newSize)
{
     delete [] ptr;
     ptr = new int[newSize]; //this works
}

int main()
{
     int* intArray = new int[5];

     resizeByReference(intArray, 10); //works as expected

     resizeByValue(intArray, 20); //after this function, intArray is deleted but not resized

     delete [] intArray; //double deletion!

     return 0;
}
imo,
you mean 'passing a pointer to a function' and the alternative 'passing a reference to a function'?
I've never seen 'passing a pointer by reference". it doesn't make sense to me. it's because I'm a beginner too?

for a beginner, there are two types of passing stg to a function;
1. pass by value
2. pass by reference
and pass by reference can be accomplished with a pointer (*) or with a reference (&)

when do you need to pass by value?
if you're going to work on a copy, not on the original. if you're passing a big object, this could slower your program.

when to use pointer or reference?
and use a pointer or reference to work on the original. no copy will be created or passed, just the address of the object (primitive data type or a class object).

if you can give a part of your code, that would be more helpful for us and you too.
Last edited on
reference to a pointer is not something that is covered in at least 4 different books I have. I dont know if its just rarely used or what. The two examples I have see of it demonstrating it was either confusing to people reading the program or could be done a better way. like the above that shacktar wrote instead of using a reference to a pointer he could have just passed a pointer and return a pointer instead. Im not saying his way is wrong. its just a demo. however i think returning a pointer would be more clear. reference to pointers is something ive just been reading these past few days. nested pointers confuse me too. like a pointer to a pointer. other then having to dereference it twice to get at the data it points to the pointer to a pointer is something i cant put together in my head. I just cant visualize it at all.
Last edited on
I have also been finding a use case for a reference to a pointer variable. Up till today, I still have not found a use for it. I was thinking maybe such feature is meant for compiler and OS developers instead ?
A reference to a pointer is useful when you want to modify the pointer.
@sohguanh

I feel that it has already been expressed in this post, but in case you still don't understand perhaps an example would help you to better understand. Run the code below and see for yourself the results

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
template<typename T>
void SwapPtrByValue(T* a, T* b)
{
	std::cout << "Swapping pointers passed by value\n";
	T* temp = a;
	a = b;
	b = temp;
}

template<typename T>
void SwapPtrByRef(T*& a, T*& b)
{
	std::cout << "Swapping pointers passed by reference\n";
	T* temp = a;
	a = b;
	b = temp;
}

template<typename T>
void OutputPointerValues(T* a, T* b)
{
	std::cout << "a points to " << *a << "\n";
	std::cout << "b points to " << *b << "\n";
}

int main ()
{
	int* a = new int(1);
	int* b = new int(2);

	OutputPointerValues(a,b);
	SwapPtrByValue(a,b);
	OutputPointerValues(a,b);

	std::cout << "\n";

	OutputPointerValues(a,b);
	SwapPtrByRef(a,b);
	OutputPointerValues(a,b);

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return 0;
}


Last edited on
Thanks for your clarification. Now I do see their use but for me I may not use so often. When a pointer variable already point to something, most of the times I want to modify that something and not modify the pointer to point to somewhere else. If that is so, then the previously pointed something would be 'lost' (unable to free it's memory) leading to leaking memory problem isn't it ?

So to play safe and unless situation called for it, I would refrain from using but it will be nice to know of this feature just in case I may need it in future.
then the previously pointed something would be 'lost' (unable to free it's memory) leading to leaking memory problem isn't it ?

There would be a memory leak only if there was a mistake in the implementation. For instance, if you dynamically allocate a pointer and pass in a reference to that pointer to a function that will change it, and if the function does not delete the pointer before re-assigning it, then yes, there will be a leak.

Or, in quirkyusername's example, if he forgot to put aside a temp variable and did a "swap" like this:

1
2
3
4
5
6
7
template<typename T>
void SwapPtrByRef(T*& a, T*& b)
{
	std::cout << "Swapping pointers passed by reference\n";
	a = b;
	b = a;
}


then yes, there would also be a memory leak because now both pointers point to "a" and the memory pointed to by "b" could not be retrieved. Saying that "using a reference to a pointer is dangerous" (or rather, that's what you were implying) is simply wrong. It is just as dangerous as using pointers, and if used correctly :) then it's not dangerous at all.
Last edited on
I admit both are dangerous but most of the times we pass by pointer in order to modify the something it points to and also for efficiency reasons if that something is a BIG object. We seldom want to change the pointer itself isn't it ? This can be reflected why in most books or tutorials this is seldom talked about maybe simply the use case for it are lesser isn't it ?

Now I know, I will keep this in mind just in case in future some requirements call for it :)
@sohguanh:

Another user for passing a pointer by reference can be to pass C strings bee reference in older compilers (Like the ones in my school).

1
2
3
4
void Func(char*& CString)
{
//somethings...
}
Topic archived. No new replies allowed.