CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2001
    Posts
    745

    Alocator-need help

    I have defined my own allocator class like:

    template<class T>
    class BufferAllocatorublic 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.

  2. #2
    Join Date
    Feb 2002
    Posts
    81
    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...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured