efficient/SuRF

Array index out of bound?

lzyLuke opened this issue · 2 comments

Hi, I am trying to rewrite a java version of SuRF. However, I encountered several problems which all involved with index out of bound exception.

In bitvector.hpp
We initially define bits_=[numWords()];

However, here

test_bits = bits_[word_id];

and here,
bits_[word_id] |= (last_word << (kWordSize - bit_shift));

we all have a chance to visit bits_=[numWords()], which is invalid. C++ actually does not raise an error or warning for this.
Although this do not cause real problems like crashing the program (not for sure), I still think this might be a problem.
Or perhaps I misunderstand the intention of doing that, so could you please give me an explanation of this?
Thanks!

Hi there,

We certainly don't want to access memory out of bound. In function "readBit", "distanceToNextSetBit", and "distanceToPrevSetBit", there is an assertion (assert(pos <= num_bits_);) at the beginning at each function to guarantee that. In function "concatenateBitvectors", the algorithm itself guarantees no out-of-bound error will occur. Hope this helps :)

Well... there are some extreme corner cases in which it indeed accesses the bound.
However, they do not modify the origin data, so there are no harms.
Thanks any way!