why pass-by-reference of a pointer variable?

X

Xiaoshen Li

Dear All,

I thought I understood using pointer variables as function parameters.
But I failed to understand why it is needed pass-by-reference of a
pointer variable.

To me, pointer variable is address variable, which holds the memory
address of the object. Using a pointer variable as a function parameter,
the function has the ability to CHANGE the object value pointed by the
pointer. Why needs pass by reference?

For example, I saw some code like the following:

void myfunctionA(int* pA);

void myfunctionB(int*& pA); //what is the advantage?

Thank you very much.
 
X

Xiaoshen Li

Thank you all. Now I think I understand much better now. I still have
one more question:

For a function taking a pointer variable as parameter, actually I saw
two ways to pass the parameter:


void myfunctionA(int* pA)
{
*pA += 1;
}

int main()
{
int *pInt = new int;
*pInt = 77;
myfunctionA(pInt);
cout << *pInt << endl; //print out 78

//another way of passing parameter

int iNum = 99;
myfunctionA(&iNum);
cout << iNum << endl; //print out 100, correct?

return 0;
}

My question is: for myfunctionB()

void myfunctionB(int*& pA)
{
*pA += 2;
}

Is the following line ok?
myfunctionB(&iNum);

I feel lost with so many &.

Thank you very much.
 
M

Mike Wahler

Xiaoshen Li said:
Dear All,

I thought I understood using pointer variables as function parameters. But
I failed to understand why it is needed pass-by-reference of a pointer
variable.

Most likely because the function will modify its value.
To me, pointer variable is address variable, which holds the memory
address of the object.

It can represent the address of an object.
Using a pointer variable as a function parameter, the function has the
ability to CHANGE the object value pointed by the pointer.

By dereferencing the pointer, yes it does. Another way would be
to use a reference instead of a pointer.
Why needs pass by reference?

This depends upon what the function does.
For example, I saw some code like the following:

void myfunctionA(int* pA);

'myfunction' can modify (and/or inspect) what 'pA' points to.
It cannot modify the caller's argument represented by 'pA'.

void myfunctionA(int *pA)
{
*pA = 0; // OK
pA = 0; // 'pA' is destroyed when function returns,
// caller's original argument is unchanged
}
void myfunctionB(int*& pA); //what is the advantage?

Again, 'myfunction' can modify (and/or inspect) what 'pA' points to.
But now, since it has a reference to the caller's argument (the pointer),
it can also modify that caller's argument.

void myfunctionB(int*& pA)
{
*pA = 0; // OK
pA = 0; // modifies caller's argument
}

int main()
{
int i = 1;
int *p = &i;
std::cout << i << '\n'; // prints 1
std::cout << p << '\n'; // prints address of 'i'

myfunctionA(p);
std::cout << i << '\n'; // prints 0

myfunctionB(p);
std::cout << p << '\n'; // prints NULL pointer value'
}

-Mike
 
G

Gianni Mariani

Xiaoshen said:
Dear All,

I thought I understood using pointer variables as function parameters.
But I failed to understand why it is needed pass-by-reference of a
pointer variable.

To me, pointer variable is address variable, which holds the memory
address of the object. Using a pointer variable as a function parameter,
the function has the ability to CHANGE the object value pointed by the
pointer. Why needs pass by reference?

For example, I saw some code like the following:

void myfunctionA(int* pA)
{
static int a[] = { 1, 2 };
pA = a;
}
void myfunctionB(int*& pA) //what is the advantage?
{
static int a[] = { 1, 2 };
pA = a;
}

One does what I expect, the other does not.

It does what this dies:

void myfunctionB(int** pA)
{
static int a[] = { 1, 2 };
*pA = a;
}
 
J

Jacek Dziedzic

Xiaoshen said:
Dear All,

I thought I understood using pointer variables as function parameters.
But I failed to understand why it is needed pass-by-reference of a
pointer variable.

To me, pointer variable is address variable, which holds the memory
address of the object. Using a pointer variable as a function parameter,
the function has the ability to CHANGE the object value pointed by the
pointer. Why needs pass by reference?

For example, I saw some code like the following:

void myfunctionA(int* pA);

void myfunctionB(int*& pA); //what is the advantage?

You can then modify the pointer itself, apart from the
pointee.
Thank you very much.

you're welcome,
- J.
 
M

Martin Vorbrodt

check this out:

template<typename T>
void better_delete(T*& ptr) {
delete ptr;
ptr = 0;
}

you delete, and set the pointer to null. this way it's, say, safe to call
better_delete twice on the same pointer :)
 
M

Mike Wahler

Xiaoshen Li said:
Thank you all. Now I think I understand much better now. I still have one
more question:

For a function taking a pointer variable as parameter, actually I saw two
ways to pass the parameter:


void myfunctionA(int* pA)
{
*pA += 1;
}

int main()
{
int *pInt = new int;
*pInt = 77;
myfunctionA(pInt);
cout << *pInt << endl; //print out 78

//another way of passing parameter

int iNum = 99;
myfunctionA(&iNum);
cout << iNum << endl; //print out 100, correct?
Correct.


return 0;
}

My question is: for myfunctionB()

void myfunctionB(int*& pA)
{
*pA += 2;
}

Is the following line ok?
myfunctionB(&iNum);

No. Because a temporary object (the pointer returned by the
& operator and passed as the argument) cannot be bound to a
non-const reference.

Write it like this and it will be OK:

void myfunctionB(int* const& pA) /* 'pA' is ref to const pointer to
non-const int */
{
*pA += 2;
}
I feel lost with so many &.

You just need to realize that '&' is used for different things
depending upon context (expressed with different syntax).

void f(int& arg); // '&' denotes a reference

int i = 42;
int *p = &i; // '&' denotes 'address of'

int j = i & 2; // '&' denotes bitwise 'and' operation

j = i && 1 // '&&' denotes logical 'and' operation

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top