Thread: Array of srtuct and shared memory

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    4

    Array of srtuct and shared memory

    Hi everybody,
    I need to share a struct of array between process using shared memory. How can I do it?

    es. of struct array I need to share:
    Code:
    struct blabla
    {
        char ch;
        int i;
        float fl;
    } bla[16]
    please help me posting an example.

    Thanks.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    What the lack of response as of yet means to say is: please describe the operating system that you are using.

  3. #3

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    4
    The O.S. I use is Unix.
    I'm able to create shared memory segment using shmget and shmat and I can share a single struct between processes. My trouble is: how to share an array af structs.

    Thanks.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are several ways. I would probably just declare a pointer to struct blabla, and set the size of the shared memory to be 16 * sizeof(struct blabla). Then use the pointer as if it was an array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    4
    Thank you matsp,
    could you please post a little example, because I am very confused on how to use pointers to an array of struct.

    Many thanks.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > because I am very confused on how to use pointers to an array of struct.
    Back to the books :-)

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by mr.toc View Post
    Thank you matsp,
    could you please post a little example, because I am very confused on how to use pointers to an array of struct.
    Many thanks.
    I think mastp means to use a pointer as if it was an array, like:
    Code:
    struct blabla* ptrBla;
    ptrBla = bla;
    ptrBla[0]; //this is bla[0]
    ptrBla[1]; //this is bla[1]
    etc etc
    the type of the array doesn't matter, being struct blabla or int makes no difference

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    4

    thanks

    Thanks a lot guys for the help. Now I understood.

Popular pages Recent additions subscribe to a feed