[LC++]Shared Memory and STL ...

George Talbot george at phat.com
Wed Apr 30 04:26:01 UTC 2003


Most STL containers accept an allocator template parameter.  You should 
see if there's a shared memory allocator that you can supply.

--George


On Tuesday, April 29, 2003, at 03:34 PM, Sandro Santos Andrade wrote:

>
>     Hi,
>
>     I'm writing two programs P1 and P2 using system V shared memory for
> inter-process communication.
>     P1 creates the shared segment, attach it to a local pointer shmptr
> (void*) and put a STL string object
>     on the attached memory:
>
>     P1:
>
>     - call shmget
>     - call shmat    :    void *shmptr = shmat (...
>     - put a STL string object on the attached memory;
>
>     string *s1 = new string ("test");
>     construct ((string *) shmptr, s1);    // runs copy constructor of 
> string
> on the shmptr memory address
>     delete s1;                                      // safe 'cause STL
> string copy constructor does a deep copy, am I wrong ?
>
>     P2:
>
>     - call shmget
>     - call shmat
>     - prints shared memory contens:
>
>     fprintf (stderr, "%s\n", ((string *) shmptr)->c_str());
>
>     But at this point, or the program prints nothing, or the program 
> crashes
> ...
>     If I get the memory contents on P1, all works fine, but P2 crashes 
> when
> reads the shared memory ...
>
>     Hmmm, an insight !! If STL string class has exogenous data (data
> represented by pointers) then this solution
> is wrong because I'll have an pointer in shared memory pointing to a 
> local
> P1 address. I'm rigth at this conclusions ???
>
>     By the way, are there any malloc implementation that allocs just 
> on the
> shared memory segment ???
>
>     Thanks in advance,
>
>     Sandro
>
>
>
> _______________________________________________
> This is the Linux C++ Programming List
> : http://lists.linux.org.au/listinfo/tuxcpprogramming List




More information about the tuxCPProgramming mailing list