Compilers and Indeterminate Argument Evaluation Order
The C standard allows implementations to evaluate function arguments in any order. Does this ever impact program behavior?
The Presumed Issue
Consider the following code snippet:
int i = 0; foo(i++, i++);
If arguments are evaluated left to right, i will be incremented twice before being passed to foo. However, if arguments are evaluated right to left, i will be incremented only once. Could this cause problems?
Compilers and Argument Evaluation
While argument evaluation order is implementation-dependent, most compilers adhere to the calling convention of the target architecture. On x86, the Pascal calling convention evaluates arguments left to right, while the C calling convention (__cdecl) evaluates them right to left.
Compilers often take this into consideration, especially for cross-platform programs, to avoid unexpected behavior.
Conclusion
Argument evaluation order is not language-dependent but platform- and implementation-dependent. While the standard does not specify the order, compilers typically follow the calling convention of the target architecture and may adjust evaluation order accordingly. This ensures that poorly written code will behave consistently, given the correct calling convention.
The above is the detailed content of Does C 's Indeterminate Argument Evaluation Order Ever Affect Program Behavior?. For more information, please follow other related articles on the PHP Chinese website!