Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > allocating stl containers in memory buffer

Reply
Thread Tools

allocating stl containers in memory buffer

 
 
Tomasz Grobelny
Guest
Posts: n/a
 
      01-01-2005
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
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      01-01-2005

"Tomasz Grobelny" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-01-2005
"Tomasz Grobelny" <(E-Mail Removed)> 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


 
Reply With Quote
 
Stu
Guest
Posts: n/a
 
      01-01-2005
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
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-01-2005
"Stu" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
nested stl containers - aliasing stl objects without invokingoperator= Andrey Vul C++ 6 10-22-2009 09:00 AM
allocating an unsigned int memory buffer for C (swig) struct njuk.njuk@gmail.com Python 0 12-05-2008 02:54 AM
Containers of iterators vs. containers of references clark.coleman@att.net C++ 7 01-25-2008 01:37 PM
Allocating vector of strings seem to crash. Allocating array ofstrings seems to be ok . Rakesh Kumar C++ 5 12-21-2007 10:42 AM
do STL containers buffer nodes to pools? Mark C++ 6 04-18-2005 02:31 PM



Advertisments