Is there a difference between an intermediate variable and a direct function call?

Is there any difference between calling the function in the opposite and calling the function and then returning the value at runtime, for example:

my function prototypes:

int aFunc(int...);
int bFunc(int...);

my first bFunc return line:

int bFunc(int...)
{
  ...
  return (aFunc(x,...));
}

my second line of return is bFunc:

int bFunc(int...)
{
  ...
  int retVal = aFunc(x,...);
  return retVal;
}
+4
source share
4 answers

A good compiler should make both identical (at least when optimization is enabled).

Theoretically, bFuncthere are two copy operations:

  • To a local variable on the stack.

  • From a local variable to the "bottom" of the stack (bottom to perspective bFunc).

retVal - ( ), , ( ), , retVal.

, , ( ), .

+1

:

return expression;

x = expression;
return x;

, , x .

++

return complicated_expression;

x = some_subexpression;
y = some_other_subexpression;
return complicated_expression_rewritten_in_terms_of_x_and_y;

: ++ , , , . , some_expression, some_other_subexpression - return - - x.

, : some_other_subexpression , some_subexpression . , , , .

+4

, - , std::vector, , .

() , , . , :

Visual ++ 8.0... : (NRVO). NRVO . , , .

+2

, .

0
source

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


All Articles