Skip to content
Prev 48508 / 63424 Next

operation on ‘numsels’ may be undefined

I don't see what's so surprising here.

That statement is identical to writing:

    if (arrMask[i] == 1) {
        numsels = ++numsels;
    } else {
        numsels = numsels;
    }

and

    numsels = ++numsels;

has two statements modifying the value of numsels (= and prefix-++) in
a single sequence point. (Do we increment then assign, or assign then
increment? The C / C++ standards leave this undefined.)

Imagine writing the operations out as functions: we have the `=`
function, and the `prefix-++` function -- both of these 'modify' (one
of) their arguments. Do we evaluate it as `=`(a, `prefix-++`(a)) or
`prefix-++`(`=`(a, a))? The C standard leaves this undefined, so
compilers are free to do what they wish (and the nice ones warn you
when there is such an ambiguity). I guess the net result of the
operation is the same in each case _here_, but this is of course not
the case for other functions that modify the value of their
operand(s). And, in truth, this is _undefined behaviour_ and so the
compiler could still rightly make demons fly out of your nose if it
wanted to upon program execution.

I highly recommend reading the slides at
http://www.slideshare.net/olvemaudal/deep-c, especially the bit on
sequence points.

Cheers,
Kevin

On Mon, Jun 23, 2014 at 9:22 PM, Kasper Daniel Hansen
<kasperdanielhansen at gmail.com> wrote: