I am trying to build a class to manage a std::vector<T*> of pointers to objects that must be contiguously allocated in the heap.
The problem I encountered is that building the vector as shown below leads to non contiguously allocated objects:
std::vector<T*> objects{};
for (size_t i = 0; i < n; i++) objects.push_back(new T());
This is because two consecutive executions of new T()
do not have to produce contiguous objects in memory. So the solution is to use new[]
operator instead, which yielded me to this implementation:
template<class T>
class HeapVector
{
private:
std::vector<T*> elems_{};
const size_t BUFFER_SIZE_{}; // Buffer size
size_t idx_{}; // Index controlling the index local to the buffer.
T * buffer_ = nullptr; // Buffer
public:
HeapVector() = default;
HeapVector(const size_t BUFFER_SIZE = 256) : BUFFER_SIZE_(BUFFER_SIZE) {};
void emplace_back() // TODO: Should pass constructor parameters or even a constructor
{
if (!(elems_.size() % BUFFER_SIZE_))
{
idx_ = 0;
buffer_ = new T[BUFFER_SIZE_];
elems_.reserve(elems_.size() + BUFFER_SIZE_);
}
// TODO: Object constructor. Must initialize buffer_[idx]
// createElement(buffer_[idx], parameters...);
elems_.push_back(buffer_ + idx_++);
}
};
By executing new T[BUFFER_SIZE_]
, I get a pointer to the first element of a contigously allocated array of BUFFER_SIZE_ elements built by using the Default Constructor.
What I want to achieve is, after this allocation is done, initialize this object with the desired parameters / another constructor (see TODOs). Also, I would like to avoid Copy Constructors.
Given that I want this class to be a templated class, what's the most generic way of achieve this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…