allocating stl containers in memory buffer

  • Thread starter Tomasz Grobelny
  • Start date
T

Tomasz Grobelny

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
 
M

Mike Wahler

Tomasz Grobelny said:
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 said:
Is
casting enough?

No.

-Mike
 
V

Victor Bazarov

Tomasz Grobelny said:
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
 
S

Stu

Mike said:
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
 
M

Mike Wahler

Stu said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top