473,324 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

allocating stl containers in memory buffer

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
Jul 22 '05 #1
4 2786

"Tomasz Grobelny" <gr***@poczta.onet.pl> wrote in message
news:ta************@oswiecenia.net...
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
Jul 22 '05 #2
"Tomasz Grobelny" <gr***@poczta.onet.pl> 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
Jul 22 '05 #3
Stu
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
Jul 22 '05 #4
"Stu" <st*@santa-li.com> wrote in message
news:79********************@comcast.com...
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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris Lambacher | last post by:
Hi, I have to do some data manipulation that needs to be fast. I had a generator approach (that was faster than a list approch) which was taking approximately 5 seconds to run both encode and...
4
by: kak3012 | last post by:
Hi, After my question today, I am not sure if this is the right group to ask but I have a simple question I guess. I have multi dimensional array called cover what I do is this iT GIVES AN...
6
by: Mark | last post by:
If you have STL containers (like list, vector,...) in functions as automatic variables, do the nodes that are put on the containers get buffered so they can be reused by later container...
4
by: Sameer | last post by:
Hello Group, This is one problem in programming that is troubling me. there is a segmentation fault just before creating memory to a structure ..i.e, just after the "allocating memory "...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
2
by: bob | last post by:
Hi, Given: 1) Custom containers that have nothing to do with vector, deque, list, map etc, 2) a custom version of new and delete defined for these containers, customNew and customDelete,...
19
by: allanallansson | last post by:
Hi i would like some guidelines for a experiment of mine. I want to experiment with the swap and ctrl-z in linux. And for this i want to create a c program that allocates almost all the free memory...
7
by: aruna.mysore | last post by:
Hi all, Is it possible to allocate an object on stack in C++ using new() operator. Thanks in advance, Aruna
10
by: Chris Saunders | last post by:
Here is the declaration of a struct from WinIoCtl.h: // // Structures for FSCTL_TXFS_READ_BACKUP_INFORMATION // typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT { union { //
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.