开发者

functions memory management C++

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.


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.


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


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.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜