I am extremely new to C ++ memory management, but I want my hands dirty to build a simple allocator that would preallocate memory for some container.
I looked at the library of Alexandrescu Loki and tried to read some blogs, but it was all just for me to understand. I want to start from some primitive and working starting point, expand it and see how it develops. This is what I have now and what I understand (starting point):
template <class T>
struct Allocator {
Allocator(){};
template <class U>
Allocator(Allocator<U> const&);
T* allocate(size_t s) {
return static_cast<T*>(::operator new(s * sizeof(T)));
}
void deallocate(T* p, size_t s) {
::operator delete(p);
}
void construct(T* p, T const& val) {
::new((void *)p) T(val);
}
void destroy(T* p) {
return ((T *) p)->~T();
}
using value_type = T;
};
So now I can use it like this:
std::vector<int, Allocator<int> > vec;
This dispenser is very simple, and I understand what it does. Now I want to expand it a bit so that my client code looks like this:
std::vector<int, Allocator<int, 8> > vec;
, 8 . :
template <class T, size_t T_num_els>
struct Allocator {
template <class U, size_t U_num_els>
Allocator(Allocator<U, U_num_els> const&);
... I keep all the rest without changes just for testing reason
, , , rebind_alloc "type". , . , ?