Thread: Segmentation fault; struct and pointer problem

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    11

    Segmentation fault; struct and pointer problem

    Hello...

    I try to create some simple posix shared memory programmed that will take some value... then
    I facing with some segmentation fault... I only have basic knowledge about struct and pointer... only learn by theory.. never used it before.. please advice

    My structure
    Code:
    typedef struct {
        unsigned int shm_hour;
        double *shm_adc;
        unsigned int *shm_dio;
    /*    pid_t pid; */
    } shmem_t;
    My idea is to store the data inside the struct.. shm_hour only have one value but shm_adc and shm_dio will have multiple input value...

    Here some of my declaration...
    Code:
    ...
    shmem_t *data;
    double *shm_adc = NULL;
    unsigned int *shm_dio = NULL;
    
    int main(int argc, char *argv[])
    {
    ...
    
        /* attach/map shared memory to our data type */
        data = (shmem_t *) shmat(shmid, NULL, 0);
    ...
    data->shm_hour = 13;        /* data is the pointer to the struct */
    data->shm_adc[0] = 0.01;
    data->shm_adc[1] = 1.23;
    data->shm_adc[3] = 3.33;
    data->shm_adc[2] = 1.52;
    data->shm_adc[7] = 2.58;
    data->shm_dio[1] = 1;
    data->shm_dio[3] = 0;
    ...
    And after debugging.. I know the problem came from shm_adc.. maybe I have wrong declaration and wrong concept.. Really appreciate if I could get some little help from all you guys..

    Code:
    Program received signal SIGSEGV, Segmentation fault.
    0x080484e4 in main (argc=1, argv=0xbffff3b4) at shm_data.c:50
    50    data->shm_adc[0] = 0.01;

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    When you write
    Code:
    double *shm_adc;
    you allocate memory for a pointer(actually an int,because the pointer memory block contains the address of where the pointer points to).The shm_adc points to nothing.So you have no permission to access where this pointer points to.You have to allocate memory,with malloc ( check for ref in google),then you can write back to this memory block.When you do not need this memory of block,you must de-allocate it with the function free. Give it a shot,and post back if needed

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please show how you are allocating memory for your pointers. It really looks like the variables in your structure should be arrays, not pointers. After all you should know how many adc and dio channels you have.

    Jim

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    11
    Quote Originally Posted by std10093 View Post
    When you write
    Code:
    double *shm_adc;
    you allocate memory for a pointer(actually an int,because the pointer memory block contains the address of where the pointer points to).The shm_adc points to nothing.So you have no permission to access where this pointer points to.You have to allocate memory,with malloc ( check for ref in google),then you can write back to this memory block.When you do not need this memory of block,you must de-allocate it with the function free. Give it a shot,and post back if needed
    So I point my shm_adc to a "NULL" is totally wrong??
    Do I really need malloc? Ok.. I will google about malloc.. but If I could get some more explanation or hint.. It could be great..
    Thanks for reply..

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    11
    Quote Originally Posted by jimblumberg View Post
    Please show how you are allocating memory for your pointers. It really looks like the variables in your structure should be arrays, not pointers. After all you should know how many adc and dio channels you have.

    Jim
    I don't allocate memory for my pointer.. I only attach the data to a shared memory...

    Code:
    #ifdef IPCCREATEATTACH
        /* create shared memory of a particular size */
        int shmid = shmget((key_t)SHM_KEY, sizeof(shmem_t), IPC_CREAT | 0666);
    
        if (shmid < 0)
        {
           perror("shmget");
           return -1;
        }
    
        /* attach/map shared memory to our data type */
        data = (shmem_t *) shmat(shmid, NULL, 0);
    
        if (data == (shmem_t *)-1)
        {
           perror("shmat");
           return -1;
        }
    #endif
    Yes I know my adc (five) and dio (eight) channel... I think I used pointer because I want to store each data into separated memory... the channel is also not sequentially...
    Last edited by sangelion; 09-19-2012 at 01:41 PM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    As the others pointed out, your problem is because you declared shm_adc as a pointer. Unfortunately, pointers don't work across shared memory, since that shared memory may be mapped to a different logical address in each process that uses it. For example, it may be at 0x4000 in process A and 0x5000 in process B, so if A stores it as 0x4000, then B tries to access data there, it may cause a seg fault in B (if that's an invalid address), or give bogus data. There are workarounds for that (storing an offset from the start of the shared memory segment and calculating when you read it out), but I think Jim's idea is best, just use an array of fixed size, since you probably know how big it will be ahead of time.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by sangelion View Post
    So I point my shm_adc to a "NULL" is totally wrong??
    Do I really need malloc? Ok.. I will google about malloc.. but If I could get some more explanation or hint.. It could be great..
    Thanks for reply..
    You set the pointer to NULL,then you allocate memory and then you set the pointer to point into that memory of block.I could explain more,but i agree with the others two.Using jim's way is easier

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    11
    Then... I will try to use jim's way... thank you for all of your suggestion... If I still have a problem after changing to array I will point it here...

    anduril462: I really don't know that I cannot define a pointer across shared memory.. thanks..

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    11
    Problem solved.. thanks guy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-27-2012, 07:05 AM
  2. Replies: 9
    Last Post: 06-02-2012, 01:36 PM
  3. Replies: 4
    Last Post: 08-29-2011, 01:21 AM
  4. Segmentation Fault With pointer to pointer strcpy
    By touchy in forum C++ Programming
    Replies: 3
    Last Post: 03-09-2011, 12:35 AM
  5. Segmentation Fault With Pointer
    By CYBERRRR in forum C Programming
    Replies: 4
    Last Post: 11-10-2010, 05:19 PM