allocating stl containers in memory buffer

Discussion in 'C++' started by Tomasz Grobelny, Jan 1, 2005.

  1. Is it possible to create for example stl queue in previously allocated
    memory buffer (memory shared between two processes)? I thought of sth like
    that:
    queue<int>* q=new(buffer) queue<int>;
    but won't integer variables (and possible others needed by queue object) be
    allocated using default allocator outside the buffer? How to force
    allocation in specified buffer? Access to shared memory is protected by
    semaphores so the should be no problem with amount of requested memory. And
    how do I force the second program to view the buffer as queue<int>? Is
    casting enough?

    Tomek
    Tomasz Grobelny, Jan 1, 2005
    #1
    1. Advertising

  2. Tomasz Grobelny

    Mike Wahler Guest

    "Tomasz Grobelny" <> wrote in message
    news:...
    > Is it possible to create for example stl queue in previously allocated
    > memory buffer


    Yes. Look up 'placement new'.

    >(memory shared between two processes)?


    Processes and sharing are outside the scope of standard C++.

    >I thought of sth like
    > that:
    > queue<int>* q=new(buffer) queue<int>;
    > but won't integer variables (and possible others needed by queue object)

    be
    > allocated using default allocator outside the buffer?


    Yes. The container object itself isn't really very large,
    the actual contained objects are not part of its 'memory
    image', they're typically only pointed to.

    >How to force
    > allocation in specified buffer?


    Use the 'allocator' template parameter for 'queue<>', to specify
    your own custom allocator which does what you want.


    >Access to shared memory is protected by
    > semaphores so the should be no problem with amount of requested memory.


    Shared memory is outside the scope of standard C++.

    >And
    > how do I force the second program to view the buffer as queue<int>?


    Store a real 'queue<int>' object there.

    > Is
    > casting enough?


    No.

    -Mike
    Mike Wahler, Jan 1, 2005
    #2
    1. Advertising

  3. "Tomasz Grobelny" <> wrote...
    > Is it possible to create for example stl queue in previously allocated
    > memory buffer (memory shared between two processes)? I thought of sth like
    > that:
    > queue<int>* q=new(buffer) queue<int>;
    > but won't integer variables (and possible others needed by queue object)
    > be
    > allocated using default allocator outside the buffer? How to force
    > allocation in specified buffer? Access to shared memory is protected by
    > semaphores so the should be no problem with amount of requested memory.
    > And
    > how do I force the second program to view the buffer as queue<int>? Is
    > casting enough?


    With standard containers there are two allocations you need to concern
    yourself with. One is the object of the type 'queue<int>' and the other
    is for every contained 'int'. First one you definitely can allocate in
    whatever buffer by using the method you showed (placement new). To use
    any special memory for the other allocation, you need to supply your
    [custom] allocator to your queue object.

    Implementing custom allocators is a subject for a chapter in a book, so do
    look it up in your favourite C++ book that deals with standard containers.

    Good luck!

    V
    Victor Bazarov, Jan 1, 2005
    #3
  4. Tomasz Grobelny

    Stu Guest

    Mike Wahler wrote:

    >>How to force
    >> allocation in specified buffer?

    >
    > Use the 'allocator' template parameter for 'queue<>', to specify
    > your own custom allocator which does what you want.


    There is no Allocator template parameter for queue<>, it's a container
    adaptor. You would need to setup a deque<>, or some other container, that
    has a custom allocator and pass that into the Container template parameter
    of queue<>.


    Stu
    Stu, Jan 1, 2005
    #4
  5. Tomasz Grobelny

    Mike Wahler Guest

    "Stu" <> wrote in message
    news:...
    > Mike Wahler wrote:
    >
    > >>How to force
    > >> allocation in specified buffer?

    > >
    > > Use the 'allocator' template parameter for 'queue<>', to specify
    > > your own custom allocator which does what you want.

    >
    > There is no Allocator template parameter for queue<>, it's a container
    > adaptor. You would need to setup a deque<>, or some other container, that
    > has a custom allocator and pass that into the Container template parameter
    > of queue<>.


    Yes, that's right. I overlooked that 'queue' is merely an
    adaptor. Thanks for the correction.

    -Mike
    Mike Wahler, Jan 1, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Sasser
    Replies:
    2
    Views:
    1,078
    jlchereau
    Nov 16, 2008
  2. Mark
    Replies:
    6
    Views:
    393
  3. Rakesh Kumar
    Replies:
    5
    Views:
    665
    James Kanze
    Dec 21, 2007
  4. Replies:
    0
    Views:
    452
  5. Andrey Vul
    Replies:
    6
    Views:
    555
    James Kanze
    Oct 22, 2009
Loading...

Share This Page