I'm now beginning to learn how to write allocators and I want to write a simple allocator that uses a provided fixed-size pool of memory.
So far I have:
template<typename T>
class PtrAllocator : public BasicAllocator<T>
{
private:
T* ptr;
public:
typedef typename BasicAllocator<T>::pointer pointer;
typedef typename BasicAllocator<T>::size_type size_type;
typedef typename BasicAllocator<T>::value_type value_type;
template<typename U>
struct rebind {typedef PtrAllocator<U> other;};
PtrAllocator(T* ptr) : ptr(ptr) {}
pointer allocate(size_type n, const void* hint = 0) {return static_cast<pointer>(&ptr[0]);}
void deallocate(void* ptr, size_type n) {}
size_type max_size() const {return 5000;}
};
int main()
{
int* ptr = new int[5000];
std::vector<int, PtrAllocator<int>> v(PtrAllocator<int>(ptr));
v.reserve(100);
delete[] ptr;
}
The above gives me the following error:
request for member 'reserve' in 'v', which is of non-class type 'std::vector<int, PtrAllocator<int> >(PtrAllocator<int>)'
I want to be able to somehow pass my ptr
to my allocator so that std::vector
uses that.
Any ideas how I can do this?
EDIT: I solved it. I had to use the following for main
:
int main()
{
int* ptr = new int[5000];
PtrAllocator<int> alloc = PtrAllocator<int>(ptr); //declared on a separate line :l
std::vector<int, PtrAllocator<int>> v(alloc);
v.resize(100);
delete[] ptr;
}