2

Namely code similar to this, making the printout undefined.

int a=41; a++ & printf("%d\n", a);

I don't know what exactly this operation is called.

halfer
  • 19,824
  • 17
  • 99
  • 186
stanigator
  • 10,768
  • 34
  • 94
  • 129
  • 3
    Notice, instead, that `a++ && printf("%d\n", a);` is perfectly defined, since `&&` introduces a sequence point - the left-hand operand of `&&` is guaranteed to be always evaluated before the right-hand one (which isn't even be evaluated if the LHO is false). – Matteo Italia May 22 '12 at 00:02
  • @MitchWheat: I would've have duplicated it if I knew the name. – stanigator May 22 '12 at 00:03
  • It's more or less a duplicate of http://stackoverflow.com/questions/949433/could-anyone-explain-these-undefined-behaviors-i-i-i-i-i-etc, although as usual this specific example isn't in that question. What's illegal about it is that it admits a valid order of evaluation in which it modifies `a` and also reads `a` for a purpose other than determining the value to be assigned, without an intervening sequence point. – Steve Jessop May 22 '12 at 00:03
  • I guess there isn't a special name for this type of undefined behavior? – stanigator May 22 '12 at 00:05
  • @stanigator: You could call it UB due to "insufficient sequence points", if you need a short phrase to refer to it. – Steve Jessop May 22 '12 at 00:06
  • 1
    @staginator: It's modifying an object and reading it without an intervening sequence point. The reason it gets asked so often is because anyone who knows what "sequence point" means knows the answer to this question, and therefore anyone that wants to know the answer doesn't know the search term. – Dietrich Epp May 22 '12 at 00:07
  • @SteveJessop: Thanks. Sorry for adding overhead. – stanigator May 22 '12 at 00:07
  • @DietrichEpp Interestingly though, this question was tagged with sequence point :). (Though I guess that does not imply that the poster knows what it means.) – Corbin May 22 '12 at 00:08
  • @Corbin: I tagged it as sequence point b/c I thought it's related to something wrong with it, although I didn't know it's a type of UB. – stanigator May 22 '12 at 00:11
  • 1
    @MatteoItalia yes definitely, or else you couldn't rely on short-circuit evaluation. – Seth Carnegie May 22 '12 at 00:15

1 Answers1

9

The problem is that it is not specified which is evaluated first, the printf or the a++, and since one has a side effect on the other (you either read a then write it then read it again, or read it then read it then write it), you get undefined behaviour.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • Note: It's been observed that some compilers are not even consistent about which way they interpret the code. – Dietrich Epp May 22 '12 at 00:09
  • 2
    @Dietrich: quite right too. The order of evaluation could depend on (for example) optimization level, or the compiler could choose a different order as likely to be most efficient in different contexts, depending on some subtle detail of the state that the compiler predicts for the instruction pipeline when the statement comes to be executed. Since order is unspecified, it could even hypothetically leave the decision to some kind of monstrous profile-guided, code-mutating runtime optimizer (aka "a modern CPU"), so the order starts out one way, then flips to the other. – Steve Jessop May 22 '12 at 00:25