small_vector icon indicating copy to clipboard operation
small_vector copied to clipboard

resize has some bugs

Open Ubpa opened this issue 4 years ago • 1 comments

void resize(size_type count, T value = T()) {
    if (count <= N) {
      // new `count` of elements completely fit on stack
      if (size_ >= N) {
        // currently, all data on heap
        // move back to stack

        /////////////////////
        // the bug is here //
        /////////////////////
        std::move(heap_.begin(), heap_.end(), stack_.begin());
      } else {
        // all data already on stack
        // just update size
      }
    } else {
      // new `count` of data is going to be on the heap
      // check if data is currently on the stack
      if (size_ < N) {
        // move to heap
        std::move(stack_.begin(), stack_.end(), std::back_inserter(heap_));
      }
      heap_.resize(count, value);
    }
    size_ = count;
  }

Ubpa avatar Mar 08 '21 07:03 Ubpa

There are actually a number of bugs with this piece of code.

  1. You forget to copy the value when expanding on stack.
  2. You forget to copy the value when expanding from stack to heap. (std::move(stack_.begin(), stack_.begin() + size_, std::back_inserter(heap_)).
  3. line 4 : if (size_ >= N) { -> if (size_ > N) {

Ubpa avatar Mar 08 '21 07:03 Ubpa