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

Jack Lloyd lloyd at acm.jhu.edu
Wed Apr 30 05:11:02 UTC 2003


It is probably going to be easier to simply put the null terminated C-style
string (as returned by .c_str()) into the shared memory segment, rather
than deal with whacked out pointer problems with putting the whole string
object into memory. The conversion to and from is very easy, -Jack

On Tue, 29 Apr 2003, 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