views:

155

answers:

4

hi everyone, i have a little bit lame question, but it's time i have this finally clear. consider regular function with some parameters and a return type.

My questions are:

  1. are there always made some copies of parameters? i mean even if the function expects reference or pointer as parameter, there are actually new references/pointers created, right? when the function is over are there some destructors called for those?

  2. is it the same with return values? is the returned value also copied from the context of the actually performed function? or are those just addresses somewhere and the value in the context is destructed too?

i probably didn't express it too clearly sooo.. if you just explained in your way how does it work with memory when some function is called i would be thankful. I have just casual idea about function of processors, but i have already dealt with assembler so there is at least something to work with.

+6  A: 

C++, like C, is a call-by-value language, so in general copies of parameters are always made.

When:

void f( int x ) {
}

is called, a copy of its parameter is made and passed to the function. When:

void f( int * x ) {
}

is called, a copy of the pointer is made and passed to the function.

The exception to this is when references are used:

void f( int & x ) {
}

no copy is made, but internally a pointer is (probably) used to pass the address of the parameter - you are not supposed to think about this however.

Exactly the same thing applies to return values:

int f() {
  return 1;
}

a copy of the value 1 is made and returned to the caller. If the function returned a pointer, a copy of the pointer would be made. Once again, references are the exception, in that no copy is made, but internally a pointer is (probably) used to return the value.

anon
great :) that's what i wanted to hear
stupid_idiot
Umm.. In general especially for integer, pointer and smaller types, it also likely to happen that they are stored in registers, both for by value calls and returns. So, yes a copy is made, but not in the RAM.
SurDin
It is probably worth mentioning that returning references to stack variables is BAAAAAD.
Dolphin
@Dolphin Indeed. Or pointers to stack values.
anon
+1  A: 

You need to look at discussions in C++ books/manuals/etc on the difference between "call-by-reference" and "call-by-value". Neil's answer is correct - the default is call-by-value, but the function in C++ (not C) can specify a specific parameter is call-by-reference.

Note also that call-by-value can cause structure copies:

struct foo f(struct bar x) { ... } 
... 
struct foo myfoo;
struct bar mybar;

myfoo = f(mybar);

f() takes a structure by value (i.e. a temporary copy is made of it, typically on the stack), and f() also returns a different structure which is copied into mybar.

jesup
A: 

constructor and destructor are called when object is created and destroyed respectively , they do not get invoked in case of reference and pointer.

In case of reference/pointer as parameters only address gets copied not the data ; whereas in case of object as parameters actual data is copied(costly in case of object with deep inheritance hierarchy).

Ashish
Depth of inheritance has nothing to do with expense of copying.
anon
+3  A: 

Neil's answer is correct, but note that compilers are allowed to optimize this. This is aptly called "copy elision". A very nice explanation of this optimization can be found at the following link:

http://cpp-next.com/archive/2009/08/want-speed-pass-by-value

Francesco
Good link; I edited it to make it clickable.
David Thornley
thanks, I always forget that :-)
Francesco