Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call by reference vs Pointer argument [duplicate]

Tags:

c++

Possible Duplicate:
FAQ: How to pass objects to functions in C++?
Pointer vs. Reference

Hi all,
in c/c++, we can pass a object as call by reference or passing pointer of the object.
for example:
i want to create a function which will take string vector as input and output a map that contains some value for each string. the return value of the function is bool, which indicate success or failure.

function (call by reference)

bool calculateSomeValue( vector<string> input, map<string, double>& result)
{
//// bla bla bla
return true/false;
}

function (using pointer )

bool calculateSomeValue( vector<string> input, map<string, double>* result)
{
//// bla bla bla
return true/false;
}

which one is best? does any one have any idea of the pros and cons of these two options?

thanks in advance.

like image 548
user619237 Avatar asked Feb 16 '11 08:02

user619237


People also ask

What is the difference between call by pointer and call by reference?

Can any one tell me the exact diff between call by pointer and call by reference. Actually what is happening on both case? Show activity on this post. The both cases do exactly the same. However, the small difference is, that references are never null (and inside function you are sure, that they are referencing valid variable).

Why do we use call by reference instead of function?

That's why to swap by function always use call by reference, and on other cases, you can use call by reference (use of pointer) where you need to reflect the effect of function on the variables declared in main functions. Top Interview Coding Problems/Challenges!

What is the difference between pointers and references in C++?

However, the small difference is, that references are never null (and inside function you are sure, that they are referencing valid variable). On the other hand, pointers may be empty or may point to invalid place in memory, causing an AV. Show activity on this post.

What is call by pointer in C++?

The call by pointer method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.


2 Answers

This is a matter of style. At Google (see Google C++ style guidelines), the following would be preferred:

bool CalculateSomeValue(
    const vector<string>& input, map<string, double>* result);

This is because using a pointer requires the explicit use of an ampersand at the call site:

 CalculateSomeValue(input, &result);

As opposed to the way it would be invoked with a reference type:

 CalculateSomeValue(input, result);

By forcing the use of an ampersand in cases where parameters are modified, it is clear at the call site what will happen. When using references, it becomes necessary to lookup the documentation for each and every function to know whether it has the potential to modify its input parameters.

However, the use of a pointer has its downsides, too. In particular, the use of a pointer means that you shift responsibility of handling null from the caller where the pointer would be dereferenced (if the variable is a pointer and not merely an address-of expression with a local variable) to the function. That is, when using a pointer type, CalculateSomeValue needs to check for nullptr (or needs to clearly document that it requires this checking in the caller), whereas the use of a reference type is self-documenting and clearly indicates that a non-null reference is expected.

For this particular situtation, I personally highly recommend a hybrid approach:

bool CalculateSomeValue(
   const std::vector<std::string>& input,
   Output<map<string, double>> result);

... where Output<T> is created by a function with the signature:

 template<typename T> Output<T> WriteTo(T& output_ref);

... and where Output<T> overloads operators ->, *, etc. This basically forces the call sites to be explicit in the fact that the input will be mutated by requiring:

 CalculateSomeValue(input, WriteTo(result));

... as opposed to:

 CalculateSomeValue(input, result);

... while simultaneously gaining the non-null semantics/syntax of references.

like image 53
Michael Aaron Safyan Avatar answered Sep 19 '22 23:09

Michael Aaron Safyan


if the argument is not optional, i find c++ many devs prefer to pass by reference. however, many like to pass by pointer solely for the visual cue. this is a bit of a holdover from c which erodes as the program grows to deal with mixed usage of references and pointers.

you can assume a reference is not null, but you should not assume that for pointers. in that case, you'll have to introduce the precondition scaffolding/checks to make sure somebody upstream did not believe the argument was optional -- if you're defensive.

personally, i prefer to pass by reference, and to use descriptive method/function/variable names (if you only detail the labels, then people have to go to the docs more frequently). this keeps the program's intention clear, and avoids the extra written checks. get* or update* may be more obvious than calculate*.

using references is consistent, well defined, and produces simpler programs since you are not dealing with mixed variable/argument types.

it really doesn't make a significant difference which you choose in this case, just be consistent in your usage. another cue i use is to put modified parameters in the same place. specifically, they precede constant arguments.

like image 44
justin Avatar answered Sep 18 '22 23:09

justin