diff --git a/include/treelite/tree_impl.h b/include/treelite/tree_impl.h index 57f0f91f..8dc385f6 100644 --- a/include/treelite/tree_impl.h +++ b/include/treelite/tree_impl.h @@ -88,11 +88,17 @@ template inline ContiguousArray ContiguousArray::Clone() const { ContiguousArray clone; - clone.buffer_ = static_cast(std::malloc(sizeof(T) * capacity_)); - if (!clone.buffer_) { - throw Error("Could not allocate memory for the clone"); + if (buffer_) { + clone.buffer_ = static_cast(std::malloc(sizeof(T) * capacity_)); + if (!clone.buffer_) { + throw Error("Could not allocate memory for the clone"); + } + std::memcpy(clone.buffer_, buffer_, sizeof(T) * size_); + } else { + TREELITE_CHECK_EQ(size_, 0); + TREELITE_CHECK_EQ(capacity_, 0); + clone.buffer_ = nullptr; } - std::memcpy(clone.buffer_, buffer_, sizeof(T) * size_); clone.size_ = size_; clone.capacity_ = capacity_; clone.owned_buffer_ = true;