[cfe-dev] Side effect of conditional operator
Sanghyeon Seo
sanxiyn at gmail.com
Tue Nov 13 13:12:28 PST 2007
2007/11/14, Chris Lattner <clattner at apple.com>:
> ?: itself doesn't have side effects. The warning is reasonable in
> this code:
>
> i < j ? (sum += i) : (sum += j);
>
> The issue is that the ?: expression itself returns a value, and that
> value is being ignored. Why not use an if statement in this case?
i = 1;
The issue is that = expression itself returns a value, and that value
is being ignored...? In this case, ?: expression as a whole clearly
has a local side effect.
The affected code is from Expat 2.0.1, xmlparse.c, lookup function,
which implements linear probing of open addressing hash table.
unsigned long h = hash(name);
unsigned long mask = (unsigned long)table->size - 1;
unsigned char step = 0;
i = h & mask;
while (table->v[i]) {
if (keyeq(name, table->v[i]->name))
return table->v[i];
if (!step)
step = PROBE_STEP(h, mask, table->power);
i < step ? (i += table->size - step) : (i -= step);
}
Yes, it can be written as i = (i - step) % table->size; (I think).
Whether this diserves a warning is a matter of opinion.
--
Seo Sanghyeon
More information about the cfe-dev
mailing list