anon
anon

Reputation:

What is more efficient: pass parameter by pointer or by value?

What is more efficient: Call by pointer or by value? I think it is call by pointer, because passing a pointer to a variable does not use as much memory as creating a copy of a variable. Am I wrong?

main(){
   int a, b;
   a = 10;
   b = 5;
   gcf(&a, &b);
   return 0;
}
int gcf(int* c, int* d){
   int val1=*c;
   int val2=*d;
   //...
}

Upvotes: 0

Views: 5780

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 310990

In your example of code snippet

main(){
   int a, b;
   a = 10;
   b = 5;
}
int gcf(int* c, int* d){
   int rem;
   int val1=*c;
   int val2=*d;
   //...
}

there is no any sense to pass variables a and b to the function indirectly using pointers if you are not going to change them. In this case the code is ineffective because that to get values of *c and *d the compiler will need to generate more instructions.

And you are wrong saying that

passing a pointer to a variable does not use as much memory as creating a copy of a variable. Am I wrong?

Usually pointers are equal to or even larger than the size of type int. For exaple in a 64-bit system sizeof( int ) can be equal to 4 while sizeof( int * ) can be equal to 8.

There is a sense to pass an object indirectly by pointers (usually in C programs) when the size of the object is much larger than the size of the pointer. In C++ instead of pointers usually references are used.

For example

#include <iostream>
#include <string>

inline void print_reverse( const std::string &s )
{
   std::cout << std::string( s.rbegin(), s.rend() ) << std::endl;
}

int main()
{
   std::string s( "Hello World" );

   print_reverse( s );
}

Here const std::string &s defined reference to an object of type std::string.

Upvotes: 1

Martin J.
Martin J.

Reputation: 5118

For types smaller than the size of a pointer (e.g. int), passing by value is more efficient.
For types bigger than the size of a pointer (e.g. most struct or class instances), passing by reference is probably more efficient (only "probably" because on top of the cost of passing the parameter, potentially constructing an object, you incur the cost of dereferencing your parameter every time you use it).

More details about passing-by-value vs. passing-by-reference can be found in this question. More details about reference vs. pointer arguments can be found in that question.

Upvotes: 4

Mats Petersson
Mats Petersson

Reputation: 129374

In nearly all code, as long as we're dealing with small/simple objects, the overhead of copying the object, vs. passing it as a pointer or reference is pretty small.

Obviously, if we make a std::string with a large chunk of text in it, it will take quite some time to copy it, relative to just passing a reference.

However, the primary objecting ANY TIME when writing code is to focus on correctness. If you have "large" objects, then use const Type &val if the value is not being modified - that way, you can't accidentally modify it. If the object is to be modified, then you NEED to use a reference or pointer to get the updates back to the caller of the function.

It is entirely possible to make code that runs noticeably slower with a reference than with a value. I was once looking into the performance of some code that we were working on, and found a function that looked something like this:

void SomeClass::FindThing(int &thing)
{
    for(thing = 0; someVector[thing] != 42; thing++)
       ;
}

It really looks rather innocent, but since each update of thing means an indirect memory access [at least in the compiler we used, which was certainly not "rubbish"], it was taking quite a lot of time out of the entire process [it was also called twice as much as necessary].

I rewrote it as:

void SomeClass::FindThing(int &thing)
{
    for(int i = 0; someVector[i] != 42; i++)
       ;
    thing = i;
}

And the function ran about 4x faster. Taking out the second, unnecessary call, as well, and we ended up with about 30% faster runtime. This was in a "fonts benchmark", and this was one out of a several dozen functions involved in the "draw fonts to screen". It's scary how a simple, innocent looking function can make a BIG difference to performance.

Upvotes: 10

Related Questions