3 ms·
I don't believe it's guaranteed to work correctly on any system, because in this code: is-member(i): return sparse[i] < n && dense[sparse[i]] == i the
by lmm 3y ago
I don't believe it's guaranteed to work correctly on any system, because in this code:
is-member(i):
return sparse[i] < n && dense[sparse[i]] == i
there's no requirement for the two sparse[i] to take the same value (since that expression evaluates to an indeterminate vaule). So the second occurrence of sparse[i] can take a value >=n, or indeed greater than the length of the whole array, at which point reading it is definitely UB.
- au8er 3y agoWould it be better to do sparse_val = sparse[i] return sparse_val < n && dense[sparse_val] == i Regardless, we are treading in undefined territory, and the behaviour is not something that can be reliably be dependent upon
- ynik 3y agoAt least in clang, your variant is not any better: uninitialized reads return the special value `undef`, which the compiler can constant-propagate to both uses of `sparse_val`. But then each `undef` can turn to a different value at each use, even if they both came from the same uninitialized read.
- saagarjha 3y agoYes, I missed that, sorry! You’re correct.