Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 30th, 2005, 04:05 PM
Xiaoshen Li
Guest
 
Posts: n/a
Default why pass-by-reference of a pointer variable?

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.

  #2  
Old December 30th, 2005, 04:25 PM
Mike Wahler
Guest
 
Posts: n/a
Default 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


  #3  
Old December 30th, 2005, 04:25 PM
Gianni Mariani
Guest
 
Posts: n/a
Default 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;
}
  #4  
Old December 30th, 2005, 04:55 PM
Jacek Dziedzic
Guest
 
Posts: n/a
Default 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.
  #5  
Old December 30th, 2005, 05:35 PM
Martin Vorbrodt
Guest
 
Posts: n/a
Default 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]


  #6  
Old December 30th, 2005, 06:45 PM
Xiaoshen Li
Guest
 
Posts: n/a
Default 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.

  #7  
Old December 30th, 2005, 07:05 PM
Mike Wahler
Guest
 
Posts: n/a
Default 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


  #8  
Old December 30th, 2005, 09:35 PM
Mateusz Łoskot
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles