Like Tree1Likes
  • 1 Post By cas

Structure inside Shared Memory [Segmentation Fault Problem]

This is a discussion on Structure inside Shared Memory [Segmentation Fault Problem] within the C Programming forums, part of the General Programming Boards category; Okay guys, first off I am new here, thanks for this forum, I guess this is a nice place to ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    10

    Structure inside Shared Memory [Segmentation Fault Problem]

    Okay guys, first off I am new here, thanks for this forum, I guess this is a nice place to get some good answers to many problems
    I will try to be clearly and fast, so I can get your help easly

    So what I am trying to do is:

    Store 30 structures inside a shared memory. Its like a app to store notes, 30 notes, like I said.
    My problem is on the iteration between structures that are on shared memory.

    My code:
    Code:
    struct note{    //Okay here is my structure for each note
           int date;
           char title[30];
           char text[900];
           char user[20];
    }
    struct nota mynote_structure;   //here I declare a note structure
    
    main(){
          struct note *ptr;   //here I declare a pointer to a structure, I will need this for the shared memory
    
          int shmID;  //here the ID for shmget
          shmID = shmget(1000, sizeof(mynote_structure) * 30, 0600 | IPC_CREAT); // I put the size of structure * 30, so I have room for 30 structures (30 notes) in that shared memory
    
          if(shmID < 0){
                 printf("Error creating shared memory");
                 exit(1);
          }
    
         ptr = shmat(shmID, NULL, 0);  // now I attach the shared memory to a pointer of type note , that is the type of my notes structure.
         
         if(ptr == NULL){
              printf("Error attaching shared memory");
              exit(1);
         }
    
         //and now i test if actually there are structures, reading the int date, that should return 0;
         printf("Date is %d", (*ptr).date); 
    
         //this should return the date of the first structure on the shared memory, if I would like the second structure date, I would do (*ptr+1).date, I guess.
    }
    The problem is that I am getting segmentation fault on that last printf, when I search for the date.

    Guys I am begginer on C, I would really appreciate some help on this, I cant get it on my mind, I study the pointers on arrays of chars, and this is how it works, I guess that it should work for structures too, that are on shared memory right?

    Thanks alot in advance,
    DarkLink

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Are you sure it's on the last printf? How do you know? Seg faults can manifest themselves somewhere other than where the pointer gets screwed up. Assuming it is there, that seg fault tells me you don't have read access to whatever value is in ptr, since *ptr is causing the seg fault. The only obvious thing I see is:

    Quote Originally Posted by man shmat
    RETURN VALUE
    On success shmat() returns the address of the attached shared memory segment; on error (void *) -1 is returned, and errno is set to indicate the cause of the error.
    You're incorrectly checking for if shmat fails, so you may have a bogus ptr. It should be if (ptr == (void *) -1).

    Do you know how to use a debugger? If not, you should leard. Then you can examine the state of your entire program as you walk through it step by step, and find what line causes the seg fault.

    Also, instead of (*ptr).date, you could use ptr->date, (ptr+1)->date. But really, since you're allocating 30 of these in a row, you basically have an array, so you should use ptr[0].date, ptr[1].date, etc.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    10
    Okay look, first I would like to thank you for you reply
    I keep awake this night trying to solve this, and I managed to not getting a segmentation fault by doing this:

    //Declaring a structure
    struct nota my_struct;

    //and after I attach shmat to the pointer ptr I do this:
    ptr = %my_struct;

    //and then when I test it to see the int on structure I do this:
    printf("My int is = %d", ptr->date);


    The result is that I get this on console:
    My int is = 1
    But why? I should get a zero!! i didnt initialize that argument of the structure, it should show me 0 . I am not getting this..
    The initialization is working..
    If I do before the printf
    ptr->date = 3;

    I get My int is = 3;
    But I why when I dont initialize it, I am not getting zero as default?
    Thanks by the way mate.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You don't get 0 because you didn't put a 0 there. C doesn't intitialize anything for you, you have to do that yourself. What you are getting is the random garbage in the memory where your struct is instantiated.

    Also; are you sure you want... ptr = %my_struct; ... not... ptr = &my_struct;...?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    10
    Thanks for your reply CommonTater
    Yes I mean & not %, but my problem is that I am getting segmentation fault when everything seems to be okay... :s
    Look my code, I am trying to store 30 structures inside shared memory..

    Code:
    #include <stdio.h>
    #include <strings.h>
    #include <string.h>
    #include <stdio.h>
    #include <sys/ipc.h>
    #include <time.h>
    #include <sys/msg.h>
    
    
    struct nota{
        int n;
        char text[30];
        char titulo[100];
        char login[20];
    };
    
    main()
    {    
        
        
        int shmID;
        struct nota *evernota;
        struct nota um;
            um.n = 1;
    
        shmID = shmget(1009, sizeof(struct nota)*30 , 0666 | IPC_CREAT);
        printf("shmID = %d\n", shmID);
        if(shmID < 0){
            printf("Failed to create shm\n");
            exit(1);
        }
        evernota =  shmat(shmID, NULL, 0);
    
    
        if(ptr_mem < 0){
            printf("Falha no attach");
            exit(1);
        }
    
        
        memcpy(&evernota[0], &um, sizeof(struct nota));
        
        printf("o meu int  %d\n", evernota[0].n);
    }
    Any clue?
    Thanks alot..

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    10
    Problem solved guys!!
    I was missing an include!! :s my bad!
    #include <sys/shm.h>

    Thanks alot all!!
    Really appreciated!

  7. #7
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    Presumably you were missing a prototype that sys/shm.h provided. If you're using gcc, the -Wall flag will catch missing prototypes. You should always use -Wall if you're using gcc. Almost every warning it provides is indicative of problematic code.
    Salem likes this.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    10
    Quote Originally Posted by cas View Post
    Presumably you were missing a prototype that sys/shm.h provided. If you're using gcc, the -Wall flag will catch missing prototypes. You should always use -Wall if you're using gcc. Almost every warning it provides is indicative of problematic code.
    Hmm didnt know about that, thanks mate!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. putting a structure in shared memory *Unix*
    By zacharoni16 in forum Linux Programming
    Replies: 3
    Last Post: 09-27-2011, 06:53 AM
  2. simple shared memory throwing segmentation fault
    By kapil1089thekin in forum C Programming
    Replies: 3
    Last Post: 10-03-2010, 07:53 AM
  3. Structure Segmentation Fault
    By GouSan in forum C++ Programming
    Replies: 4
    Last Post: 12-05-2007, 01:06 AM
  4. Shared Memory Segmentation Error
    By doom in forum C Programming
    Replies: 3
    Last Post: 12-11-2005, 06:59 AM
  5. Replies: 7
    Last Post: 12-10-2004, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21