Thread: Shared memory, cannot read last element in a struct

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Göteborg
    Posts
    28

    Shared memory, cannot read last element in a struct

    Hi,

    I have trouble reading the last element in a struct.
    I do get the correct value for the first two elements. In my example that is:
    a = 11 and c = H
    however I get:
    b = 0
    but I am expecting b=2

    I got two files to handle this.
    The first one is writing data to memory -
    Code:
    #include <sys/shm.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct {
        int  a;
        char c;
        int  b;
    } MyObject;
    
    
    int main() {
    
        int      shmid;
        key_t    key;
        char     *shm;
        MyObject obj = {11,'H',2};
    
        key = ftok("/tmp",'k');
        printf("key in server: %d\n",key);
    
        // create the segment.
        shmid = shmget(key, sizeof(obj), IPC_CREAT | SHM_R | SHM_W);
        printf("shmid:         %d\n",shmid);
    
        // attach the segment to our data space.
        shm = (char*)shmat(shmid, NULL, 0);
        printf("shm:           %d\n",shm);
    
        // put some things into the memory for the other process to read.
        memcpy(shm,&obj,sizeof(obj));
        printf("Data written to memory\n");
    
        return 0;
    }
    And the second one is reading from memory -
    Code:
    #include <sys/shm.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct {
        int  a;
        char c;
        int  b;
    } MyObject;
    
    int main() {
    
        int      shmid;
        key_t    key;
        char     *shm;
        MyObject *obj2;
    
        key = ftok("/tmp",'k');
        printf("key in client: %d\n",key);
    
        // locate the segment.
        shmid = shmget(key, sizeof(obj2), SHM_R | SHM_W);
        printf("shmid:         %d\n",shmid);
    
        // attach the segment to our data space.
        shm = (char*)shmat(shmid, NULL, 0);
        printf("shm:           %d\n",shm);
    
        obj2 = (MyObject*)malloc(sizeof(obj2));
    
        // read what the server put in the memory.
        memcpy(obj2,shm,sizeof(obj2));
    
        printf("a: %d\n",obj2->a);
        printf("c: %c\n",obj2->c);
        printf("b: %d\n",obj2->b);
    
        return 0;
    
    }
    So my question is why I cannot get correct value for the third element in my struct?
    In the second file where I'm reading from memory I allocate some space. Is this incorrect in some way?
    I'm running this on a Linux machine.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    All instances of sizeof(obj2) in your "second one" need to be sizeof(*obj2).

    The reason is that sizeof(obj2) is the size of a pointer, which is independent of the size of a MyObject (and likely to be smaller than sizeof(MyObject)). Since obj2 is a pointer to MyObject, sizeof(*obj2) is equal to the sizeof(MyObject).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Location
    Göteborg
    Posts
    28
    Aha! That whas the problem.
    Yes, sizeof(obj2) gave me 8 and sizeof(*obj2) gave me 12.

    Thank you for your help.

    Now it's working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Putting A Struct Into Shared Memory
    By Lockout in forum C Programming
    Replies: 1
    Last Post: 02-28-2011, 03:05 AM
  2. accessing struct element from another struct
    By creek23 in forum C Programming
    Replies: 10
    Last Post: 06-24-2010, 02:56 AM
  3. cannot read key when allocating shared memory
    By hjr in forum C Programming
    Replies: 1
    Last Post: 10-19-2009, 11:08 AM
  4. problem with sending array of struct over shared memory
    By jet-plane in forum C Programming
    Replies: 26
    Last Post: 05-10-2008, 04:10 AM
  5. shared memory can not read value over 255
    By jbsloan in forum C Programming
    Replies: 4
    Last Post: 04-03-2005, 11:56 AM