1

For example, we have

int p(void) {
  return 4;
}

int q(void) {
  return 5;
}

int main(void) {
  int x = p() + q();
  return 0;
}

How does the stack frame look like in this case? To be exact, are p and q evaluated simultaneously, or after p is first evaluated to be 4, the program proceeds to q?

Jude Gao
  • 11
  • 2
  • 3
    1. In single threaded environment only one function can be executed at a time. 2. Formatting your code does not change compilation result. – Slava Feb 08 '18 at 14:15
  • Compile it to assembly if you're curious about stack frame or such low-level things. – user202729 Feb 08 '18 at 14:18
  • Any order is allowed in most C++ operators (except, for example, `||` and `&&` which are short circuiting). But in this case any modern compiler with optimizations turned on will just remove everything you wrote, because it has no side effects. – Groo Feb 08 '18 at 14:18
  • It depends on the compiler, the compiler flags and other factors. To find out check it yourself with your favorite debugger. – Jabberwocky Feb 08 '18 at 14:20

1 Answers1

4

from cppreference

Order of evaluation of the operands of almost all C++ operators (including the order of evaluation of function arguments in a function-call expression and the order of evaluation of the subexpressions within any expression) is unspecified. The compiler can evaluate operands in any order, and may choose another order when the same expression is evaluated again.

There are exceptions to this rule which are noted below.

Except where noted below, there is no concept of left-to-right or right-to-left evaluation in C++. This is not to be confused with left-to-right and right-to-left associativity of operators: the expression f1() + f2() + f3() is parsed as (f1() + f2()) + f3() due to left-to-right associativity of operator+, but the function call to f3 may be evaluated first, last, or between f1() or f2() at run time

mightyWOZ
  • 3,012
  • 1
  • 19
  • 29
  • Note that after C++17 things have been tighted up a tad. For example, `f(a()+x(),b(),c)` -- in C++17 you cannot evaluate `b()` between `a()` and `x()` and `+`. I do not recall if similar restrictions where applied to `+`; if it was, then the order of `f1()+f2()` vs `f3()` was unspecified, but you would no longer be able to evaluate `f3()` between `f1()` and `f2()`. I do not have the new rules memorized, but you should be aware that they have changed, so cppreference may be out of date. – Yakk - Adam Nevraumont Feb 08 '18 at 14:40
  • @Yakk could you please point me to the specific section which talks about these changes. i was lookin at C++ 17 specification and i could only find this from section 8.2 "The initialization of a parameter, including every associated value computation and side effect, is indeterminately sequenced with respect to that of any other parameter." – mightyWOZ Feb 08 '18 at 15:20
  • https://stackoverflow.com/questions/38501587/what-are-the-evaluation-order-guarantees-introduced-by-c17 -- that SO post contains more than I know. – Yakk - Adam Nevraumont Feb 08 '18 at 15:23