Click to See Complete Forum and Search --> : Alocator-need help


Kohinoor24
June 24th, 2002, 03:29 PM
I have defined my own allocator class like:

template<class T>
class BufferAllocator:public allocator<T>

I have defined all the needed functions for the allocator.Out of them is the

pointer allocate(size_type _N, const void* p = NULL)
throw (DocGenEx)
{
return reinterpret_cast<pointer>(m_buffer.allocateBytes(_N * sizeof(T)));
// here m_buffer is another class where allocateBytes() function is defined.
}

The following is the class "T" for the allocator


class NgDocData
{
public:

list<NgSubDocData<B>> m_ngSubDocDataList;
};

Will the allocate function return me with a ptr to this object.

So can I be able to do something like this:

NgDocData * p = allocate();// which returns the ptr to this object
p->m_ngSubDocDataList;

Is this okay & could anyone tell me about how I should use the construct function of the allocator also with a code snippet

Thanks in advance.

cpitis
June 26th, 2002, 02:06 AM
The STL containers use to allocate space for more objects, when the container overflows. For example, the std::vector will double its size at every overflow. So, the allocator will reserve memory. The construct method is used to construct an object (using its copy constructor) to an already allocated space. The destruct will call the object destructor, but without deallocating the memory.

The following code I extracted from std::vector class. It fills the vector starting with the element pointed by F, using the X value. The number of filled elements is N:

void _Ufill( iterator F, size_type N, const T &X)
{
for (; 0 < N; --N, ++F)
allocator.construct( F, X);
}

You can always take a look in the STL implementation you use. I worked to some allocators, too. The STL code was a real help for me...