small_vector
small_vector copied to clipboard
resize has some bugs
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;
}
There are actually a number of bugs with this piece of code.
- You forget to copy the
valuewhen expanding on stack. - You forget to copy the
valuewhen expanding from stack to heap. (std::move(stack_.begin(), stack_.begin() + size_, std::back_inserter(heap_)). - line 4 :
if (size_ >= N) {->if (size_ > N) {