+ 3

C++ questions in quiz section

There are lots of c++ questions of the type "guess the output" but are using two or more combination of postfix or prefix operators in an single expression. As per C++ standards, the order of evalution of operands in such expressions is undefined i.e. in int x=0; int z = ++x + x++; the value of z is undefined, because the order of evaluation is undefined, hence the result is undefined or compiler specific. Hence i think all such questions should be removed from c++ quiz section. What's your thought guys?

9th Jun 2017, 6:09 PM
NeutronStar
NeutronStar - avatar
2 Respuestas
+ 9
I agree NeutronStar. I have made the same remark a while ago. There is nothing to learn in undefined behaviour quizzes - except to know that it is machine/compiler dependant.
9th Jun 2017, 7:53 PM
Karl T.
Karl T. - avatar
+ 4
The format may not lend itself to highlighting its educational value: * It affects all compilers (a useful detail)...because the operations occur simultaneously (at odds with each other) in the same "sequence point". * Knowing that "sequence points" exist teaches how programming languages are made, how compilers are designed, optimize, etc. * If this never came up and then one day it was in your codebase...would it be harder to debug? * Knowing this is interesting+helpful in information security, should someone leverage a specific compiler's behavior. * Another way to look at this is "unreliable". While you can't depend on the behavior across compilers, see the previous bullet.
9th Jun 2017, 7:04 PM
Kirk Schafer
Kirk Schafer - avatar