Re: [Linux] Shared memory and pointers

Discussion in 'C++' started by BGB, May 14, 2012.

  1. BGB

    BGB Guest

    On 5/14/2012 8:17 AM, Willy wrote:
    > Hi,
    > I've a problem with shared memory and pointers.
    >
    > I've a struct like this in shm:
    >
    > typedef struct my_struct
    > {
    > char mat[3][10];
    > char * p_mat;
    > }my_struct;
    >
    > and a process A that sets:
    >
    > shm->p_mat = &shm->mat[2];
    >
    > Another process B that try to print it:
    >
    > printf("%s",shm->p_mat);
    >
    > I obtain a segmentation fault.
    >
    > I think that is not the right way to use pointers in shared memory, but
    > I can't find more info.
    >


    technically OT here, but I will answer:
    part of the problem here is that, unless otherwise specified, the
    shared-memory will not necessarily be mapped to the same address in
    every process.

    as a result, pointers in one process will not be correct in another
    (since all the memory will be in a different location, ...).

    instead, other options are needed, such as:
    storing pointers as offsets relative to the mapping base address;
    storing pointers as relative offsets from the address of the pointer.

    also note that pointers to any memory outside the shared-memory can't be
    safely used, as they may not exist in the other process.

    or such...
    BGB, May 14, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zorro
    Replies:
    3
    Views:
    535
    Venu Yanamandra
    Nov 25, 2010
  2. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    651
  3. Victor Bazarov

    Re: [Linux] Shared memory and pointers

    Victor Bazarov, May 14, 2012, in forum: C++
    Replies:
    0
    Views:
    441
    Victor Bazarov
    May 14, 2012
  4. Juha Nieminen
    Replies:
    0
    Views:
    410
    Juha Nieminen
    May 15, 2012
  5. Nobody
    Replies:
    0
    Views:
    412
    Nobody
    May 16, 2012
Loading...

Share This Page