Is passing pointer argument, pass by value in C++?
Asked Answered
C

5

44

Is passing pointer argument, pass by value in C++? Since i see that any change to the pointer as such is not reflected outside the method. The changes i do by dereferencing the pointer is reflected though.

In that case, is it acceptable/standard procedure to use pointer to pointer as argument to a function to modify the pointer value as such within a function?

Camber answered 13/12, 2010 at 7:3 Comment(1)
The pointer is passed by value but the object pointed-to by the pointer can be modified by the function (const-qualification depending).Arnica
M
56

Yes to both.

Pointers are passed by value as anything else. That means the contents of the pointer variable (the address of the object pointed to) is copied. That means that if you change the value of the pointer in the function body, that change will not be reflected in the external pointer that will still point to the old object. But you can change the value of the object pointed to.

If you want to reflect changes made to the pointer to the external pointer (make it point to something else), you need two levels of indirection (pointer to pointer). When calling functions it's done by putting a & before the name of the pointer. It is the standard C way of doing things.

When using C++, using references is preferred to pointer (henceforth also to pointer to pointer).

For the why references should be preferred to pointers, there is several reasons:

  • references introduce less syntaxic noise than pointers in function body
  • references keep more informations than pointers, than can be useful for compiler

Drawbacks of references are mostly:

  • they break the simple pass-by-value rule of C, what makes understanding the behavior of a function regarding of parameters (will they be changed ?) less obvious. You also need function prototype to be sure. But that is not really worse than the multiple pointer levels necessary when using C.
  • they are not supported by C, that can be a problem when you write code that should work with both C and C++ programs (but that's not the most usual case).

In the specific case of pointer to pointer, the difference is mostly simplicity, but using reference it may also be easy to remove both levels of pointers and pass only one reference instead of a pointer to pointer.

Mana answered 13/12, 2010 at 7:5 Comment(4)
@Arafangion: I edited my answer to explain why preferences should be preferred to pointers.Mana
I suppose it's subjective - I prefer using references because I move the responsibility for null pointer checking to the caller, and I usually don't care about C compatibility. However I'm not sure I'm sold on "You also need function prototypes to be sure that the value is not changed" - that's true for many of C++'s classes - they could be pointers themselves and might or might not be sharing data. That said, I do prefer pass by value and letting the class take care of any pointers if any are needed, unless I'm returning rvalue references that should be immediately set, regardless, +1Sabbat
@Arafangion: the pointer validity checking place in C is also a matter of calling convention. I usually perform it at caller level for inner functions and check at malloc or external entry points for lib. But indeed with reference, checking a variable is not NULL sounds and looks dumb. But it is also not less dumb when using C when you pass the address of some automatic array and the first thing done by callee code is just checking it's not NULL.Mana
This knowledge has been helping me a lot.Bibbs
P
14

I understand the confusion here. The concepts of "pass by value" and "pass by reference" are not so clear even if they seem to be so. Bear in mind that the computer does not know these concepts and does not behave according to it. The computer does not know about the types. Hence it does not make a distinction of pointers and values. Let me try to explain by and example:

void func1(int x) //copy some value to local variable x (of type int)
{
   x = 5; //modify local variable. lost after function call
}

void func2(int *x) //copy some value to local variable x (of type int*)
{
   int a;
   x = &a; //modify local variable. lost after function call.
}

void func3(int *x) //copy some value to local variable x(of type int*)
{
   *x = 10; //x is local but *x is not! change is saved after function call!
}

func1 and func2 are identical. Both modify a local variable. Modification is lost after function is popped off the stack. func3 has ability to change another memory location (a variable which is not local to the function).

basically, every function call is "call by value". But in the case of a pointer type, we have a way to change the content of a remote address in memory.

Phionna answered 13/12, 2010 at 9:59 Comment(1)
This is so great and weard at the same time man. Thaks for the explanaton!Bibbs
T
2

Pass by value using Pointers I'll explain it by example:

void f(int *ptr)
{
   cout<<*ptr;
}


int main ()
{
   int a=10;
   int *aptr=&a;
   f(aptr);
   return 0;
} 

Here, in main function a is an integer variable whose content is 10 and address is 00F8FB04 (assume). aptr is pointer to integer, that store the address of integer variable a, so aptr content is address of integer variable a that is 00F8FB04. When we pass aptr as the function argument only content of aptr (that is address) are copies to function parameter. So, ptr will receive the copy of content of aptr (that is address 00F8FB04)

Trixy answered 23/6, 2019 at 7:54 Comment(1)
Yes, the address passed to f is a copy. But since that's a copy of an address, the content in that address can be modified by f :)Bibbs
H
0

Either a pointer to a pointer, or a reference to a pointer, is what you would use if you wanted to potentially change the pointer itself. To your original question, technically, yes, all parameters are passed by value.

Hightension answered 13/12, 2010 at 7:6 Comment(1)
"All parameters are passed by value" No, arguments passed by reference are not passed by value.Pinot
K
0

Yes it is, as it is in C.

In that case, is it acceptable/standard procedure to use pointer to pointer as argument to a function to modify the pointer value as such within a function?

In which case? What do you want? You can use real references with the & modifier.

void func(type &ref);
Keele answered 13/12, 2010 at 7:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.