c++ - Order of execution of function calls within a line - undefined? -
p.77 k&r c says output of next programme undefined, current version of xcode gives no warning. still undefined in current standard? in c++? bonus points similar programme in java , c#.
#include <stdio.h> int my_m() { static int n = 0; static int m[] = { 42, 57 }; homecoming n++ % 2 == 0 ? m[0] : m[1]; } int main() { printf("%d" , my_m() - my_m()); homecoming 0; }
the result isn't undefined, unspecified.
the difference undefined behavior means anything can happen, including behavior seems exclusively unrelated in programme @ all. classic line "makes demons fly out of nose."
unspecified means have no way of knowing which phone call of my_m evaluated first , evaluated second, but be1 1 invocation , other. 1 phone call start , run completion other phone call start , run completion--the 2 won't interleaved or that. thing that's unpredictable order in 2 calls made (and yes, under current c , c++ standards, order still 100% unpredictable).
in both c# , java order of evaluation specified left right.
1. or @ to the lowest degree it'll deed if did. both c , c++ follow what's called "as if rule" allows compiler things differently long in way maintains specified externally visible behavior.
c++ c
No comments:
Post a Comment