C ++ memory management functions

I have a little lame question, but this is the time when I finally understood. consider a regular function with some parameters and return type.

My questions:

  • Are there always any copies of the parameters? I mean, even if a function expects a link or pointer as a parameter, there are actually new links / pointers, right? when the function is finished, are there some destructors called for them?

  • Is it the same with return values? Is this return value also copied from the context of the actual function being executed? or are these addresses just somewhere, and the meaning in the context also destroyed?

I probably did not express it too clearly. If you simply explained on your way how this works with memory when a function is called, I would be grateful. I just have a random idea about processor functions, but I already dealt with assembler, so there is at least something to work with.

+4
source share
4 answers

C ++, like C, is the default language, so in the general case, copies of the parameters are always saved.

When:

void f( int x ) { } 

its parameter is copied and passed to the function. When:

 void f( int * x ) { } 

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

The exception is the use of links:

 void f( int & x ) { } 

there is no copy, but inside the pointer (probably) is used to pass the parameter address - you should not think about it, 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 returns a pointer, a copy of the pointer will be created. References are again an exception, since no copy is made, but internally a pointer (probably) is used to return the value.

+6
source

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

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

+3
source

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

Note also that calling by value can cause copies of the structure:

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

f () takes a structure by value (i.e., it makes a temporary copy, usually on the stack), and the f () function also returns another structure that is copied to mybar.

+1
source
Constructor and Destructor

called when the object is created and destroyed accordingly, they are not called in the case of a reference and a pointer.

In the case of a link / pointer as parameters, only the address is not copied by data; whereas in the case of an object, actual data is copied as parameters (expensive in the case of an object with a deep inheritance hierarchy).

0
source

Source: https://habr.com/ru/post/1299655/


All Articles