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

Sandro Santos Andrade sandro.andrade at sixbell.com.br
Wed Apr 30 03:32:02 UTC 2003


    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:


    - 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 ?


    - 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,


More information about the tuxCPProgramming mailing list