20

I recently ran across the following situation:

#include <iostream>

int *p = 0;

int f() {
    p = new int(10);
    return 0;
}

void g(int x, int *y = p) {
    std::cout << y << std::endl;
}

int main() {
    g(f());
}

This is quite subtle, since you usually don't expect the default arguments to change during their evaluation for the function call. I had to take a look at the assembly to spot this error.

Now my question is: Is this really undefined behavior, since there aren't any guarantees concerning the evaluation order of function arguments?

2 Answers 2

15

The order of evaluation (i.e. determining the value) of function arguments is not specified. The compiler is free to execute them in any order, and even intermingled if there are no other factors stopping it from doing so.

Evaluation of default arguments happens in the context of the caller, not the callee. So the call to f() is necessary for one argument, and reading the global variable p for the other. Which order this happens in is not specified, so the global could be read before or after the call to f().

3
  • "Evaluation of default arguments happens in the context of the caller" Do you have a reference in the current C++ standard for this claim?
    – phlipsy
    Aug 26, 2015 at 10:55
  • 1
    There's a non-normative note in 1.9p11 that says it explicitly. Other than that, I think it's only implied by lack of distinction from other arguments. Aug 26, 2015 at 11:25
  • 2
    @phlipsy see [dcl.fct.default]/9 "Default arguments are evaluated each time the function is called. The order of evaluation of function arguments is unspecified.", I think this clearly implies that g(f()) is the same as g(f(), p)
    – M.M
    Aug 26, 2015 at 11:26
14

If I understand it correctly, your call

    g(f());

is equivalent to

    g(f(), p);

due to the declaration

    void g(int x, int *y = p);

And arguments to the g function , f() and p, can be evaluated in any order, so you can get g called with y assigned either zero (if p is evaluated first, then it returns its initial value) or the newly allocated array pointer (if f() is evaluated first and it assigns a new value to p as its side effect).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.