Find the answer to your Linux question:
Results 1 to 2 of 2
I have what should be a relatively simple program (fadec.c) that maps a struct from an included header file (fadec.h) to a shared memory region, but I’m struggling accessing members ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2011
    Posts
    1

    Dereferencing pointer to a shared memory struct


    I have what should be a relatively simple program (fadec.c) that maps a struct from an included header file (fadec.h) to a shared memory region, but I’m struggling accessing members in the struct from the pointer returned by shmat. Ultimately, I want to access members in the shared memory structure with a globally declared version of the struct, ‘shm__’. Not only do I not know how to accomplish that, but I can’t even seem to access members of the shared struct directly from the pointer itself (compiler complains about ‘dereferencing pointer to incomplete type’). I’m at a loss and could use another set of eyes if you guys don’t mind taking a gander:


    Compile Errors:
    tony-pc:/cygdrive/p/test> cc -o fadec fadec.c
    fadec.c: In function 'main':
    fadec.c:30: error: dereferencing pointer to incomplete type
    fadec.c:31: error: dereferencing pointer to incomplete type


    fadec.h:
    struct io_struct // sample, full structure is closer to 16384 bytes (see below)
    {
    int number;
    };


    fadec.c:
    #include <sys/shm.h>
    #include <sys/ipc.h>
    #include <sys/types.h>
    #include <string.h>
    #include <stdio.h>
    #include "fadec.h"

    #define SHM_KEY 1000

    struct iostruct *test; // pointer to structure
    volatile struct io_struct shm__; // this is ultimately how I want to reference the
    // struct in shared memory

    int main(int argc, char *argv[])
    {
    int shmid = shmget(SHM_KEY, 16384, IPC_CREAT);
    if( shmid == -1) return -1;

    test = shmat(shmid, NULL, 0);
    if((int)test == -1)
    {
    shmctl(shmid, IPC_RMID, NULL);
    return -1;
    }
    printf("Memory attached at %X\n", (int)test);
    fprintf(stderr,"test = %X\n",test);

    // Need to somehow map 'test' to 'shm__' so that I can access with: shm__.number = 0;
    // But, short of that, just try to set a value through 'test' directly:
    (* test).number = 0; // or should be able to use ‘test->number=0;’ here
    fprintf(stderr,"(* test).number = %d\n",(* test).number);

    return(0);
    }


    Thanks for your help!
    Tony
    Last edited by arette; 02-05-2011 at 06:40 PM.

  2. #2
    Just Joined!
    Join Date
    Feb 2011
    Posts
    12
    I don't know what you're trying to do, but I believe,

    Code:
    struct iostruct *test; // pointer to structure
    Should be,

    Code:
    struct io_struct *test; // pointer to structure
    Then the code compiles.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •