Connecting Tech Pros Worldwide Forums | Help | Site Map

why pass-by-reference of a pointer variable?

Xiaoshen Li
Guest
 
Posts: n/a
#1: Dec 30 '05
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.


Mike Wahler
Guest
 
Posts: n/a
#2: Dec 30 '05

re: why pass-by-reference of a pointer variable?



"Xiaoshen Li" <xli6@gmu.edu> wrote in message
news:dp3lct$18mi$1@osf1.gmu.edu...[color=blue]
> 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.[/color]

Most likely because the function will modify its value.
[color=blue]
>
> To me, pointer variable is address variable, which holds the memory
> address of the object.[/color]

It can represent the address of an object.
[color=blue]
>Using a pointer variable as a function parameter, the function has the
>ability to CHANGE the object value pointed by the pointer.[/color]

By dereferencing the pointer, yes it does. Another way would be
to use a reference instead of a pointer.
[color=blue]
> Why needs pass by reference?[/color]

This depends upon what the function does.
[color=blue]
>
> For example, I saw some code like the following:
>
> void myfunctionA(int* pA);[/color]

'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
}
[color=blue]
>
> void myfunctionB(int*& pA); //what is the advantage?[/color]

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


Gianni Mariani
Guest
 
Posts: n/a
#3: Dec 30 '05

re: why pass-by-reference of a pointer variable?


Xiaoshen Li wrote:[color=blue]
> 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)[/color]
{
static int a[] = { 1, 2 };
pA = a;
}[color=blue]
>
> void myfunctionB(int*& pA) //what is the advantage?[/color]
{
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;
}
Jacek Dziedzic
Guest
 
Posts: n/a
#4: Dec 30 '05

re: why pass-by-reference of a pointer variable?


Xiaoshen Li wrote:[color=blue]
> 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?[/color]

You can then modify the pointer itself, apart from the
pointee.
[color=blue]
> Thank you very much.
>[/color]

you're welcome,
- J.
Martin Vorbrodt
Guest
 
Posts: n/a
#5: Dec 30 '05

re: why pass-by-reference of a pointer variable?


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 :)



"Xiaoshen Li" <xli6@gmu.edu> wrote in message
news:dp3lct$18mi$1@osf1.gmu.edu...[color=blue]
> 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.
>[/color]


Xiaoshen Li
Guest
 
Posts: n/a
#6: Dec 30 '05

re: why pass-by-reference of a pointer variable?


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.

Mike Wahler
Guest
 
Posts: n/a
#7: Dec 30 '05

re: why pass-by-reference of a pointer variable?



"Xiaoshen Li" <xli6@gmu.edu> wrote in message
news:dp3ujt$1dpj$1@osf1.gmu.edu...[color=blue]
> 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?[/color]

Correct.
[color=blue]
>
> return 0;
> }
>
> My question is: for myfunctionB()
>
> void myfunctionB(int*& pA)
> {
> *pA += 2;
> }
>
> Is the following line ok?
> myfunctionB(&iNum);[/color]

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;
}
[color=blue]
>
> I feel lost with so many &.[/color]

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


Mateusz Łoskot
Guest
 
Posts: n/a
#8: Dec 30 '05

re: why pass-by-reference of a pointer variable?


Xiaoshen Li wrote:[color=blue]
>
> For example, I saw some code like the following:
>
> void myfunctionA(int* pA);
>
> void myfunctionB(int*& pA); //what is the advantage?
>[/color]

I hope this will be helpful:
http://www.codeproject.com/cpp/PtrToPtr.asp

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Closed Thread