0

Okay look first thanks for this place :) I would like to create a piece of shared memory to store 30 structures of the same type, but I am getting errors on compile..

struct nota
{
    int n;
    char text[30];
    char titulo[100];
    char login[20];
}

main(){
     int shmID;
     struct nota *evernota;
     struct nota um;

     shmID = shmget(1009, sizeof(struct nota)*30 , 0666 | IPC_CREAT);

     evernota = shmat(shmID, NULL, 0);

     evernota[0] = &um;  //Declaring one note here to the first position of shm..
     evernota[20] = &um;  //Declaring the same note on the 20 position of shm..

     printf("o meu int é %d\n",evernota[0]->n);  //Here I would get the int n of each structure, that should be 0, because its not initialized..
     printf("o meu int é %d\n",evernota[20]->n);  //and there the same n, because I use the same structure on position 0 and 20..  
}

But I have compile errors, someone see where is the problem?? Thanks alot in advance!!!

1
  • Are you missing some #includes? Commented Nov 5, 2011 at 11:56

3 Answers 3

1

well for starters you need the includes:

#include <stdio.h>
#include <string.h>
#include <sys/shm.h>

then you need the semicolon after struct definition:

struct nota {
    ...
};

Add the flag IPC_EXCL, makes shmget fail if the memory key is already in use (use IPC_PRIVATE instead of 1009 to guarantee the memory segment is new). Then a check on shmID != -1 would be nice, and to copy the references use memcopy:

memcpy(&evernota[0], &um, sizeof(struct nota));  
memcpy(&evernota[20], &um, sizeof(struct nota));

about the printfs, use the . instead of ->. They will print garbage data, they're not automatically 0-initialized on allocation, you have to do it by yourself.

Sign up to request clarification or add additional context in comments.

4 Comments

I already had the includers, and also the checks for both shmget and shmat.. I keep getting segmentation fault..
try adding the flag IPC_EXCL, that makes shmget fail if the returned segment is already in use: shmget(IPC_PRIVATE, sizeof(struct nota)*30 , 0666 | IPC_CREAT | IPC_EXCL)
well, about the code you linked to Il-Bhima: 1- missing #include <sys/shm.h> (stdlib.h would be nice too), 2- missing semicolon after struct 3- ptr_mem not declared (think you meant evernota) Plus: error exit should return negative values, better specify the type returned by main.
yes the problem was the missing includer <sys/shm.h> :s thanks alot mate!!
1

Variable um of struct nota type is not initialized. What do you expect to be printed to stdout?

Check missed headers, they should be at least:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

To initialize one element of an array use memcpy:

memcpy(&evernota[0], &um, sizeof(struct nota));

It's always good style to check return values of system function:

   if ((shmID = shmget(1009, sizeof(struct nota)*30 , 0666 | IPC_CREAT) < 0) {
        perror("shmget");
        exit(1);
   }

2 Comments

I have the checks, I get segmentation fault when I try to printf the evernota[0].n .. thanks :s
Does shmat() returns valid address?
1

Ok, there is a lot wrong with this code. You at least need the following includes:

 #include <sys/shm.h>
 #include <stdio.h>

The second problem is that you are not initialising um or evernote at all. This means that for example, evernote[0]->n will contain garbage data. So you should at least have, for example

um.n = 1;

Now comes the problem of copying um to the shared memory segment. You need to copy the contents of the memory defined by the um struct into the evernota array. To do this:

 memcpy(&evernota[0], &um, sizeof(struct nota));  
 memcpy(&evernota[20],&um,sizeof(struct nota));  

NB: memcpy is defined in string.h.

Now finally, to print the contents of the field n in evernota[0] you need only use the dot operator i.e.

 printf("o meu int é %d\n",evernota[20].n);  

I think thats everything.

EDIT: Does the code below still give you segfaults?

   #include <sys/shm.h>
   #include <string.h>
   #include <stdio.h>

   struct nota {
     int n;
     char text[30];
     char titulo[100];
    char login[20];
  };

     int main(){

        int shmID;
        struct nota *evernota;
        struct nota um;

        um.n = 1;
        shmID = shmget(1009, sizeof(struct nota)*30 , 0666 | IPC_CREAT);

        evernota = shmat(shmID, NULL, 0);

        memcpy(&evernota[0], &um, sizeof(struct nota));  
        memcpy(&evernota[20],&um,sizeof(struct nota));  

        printf("o meu int é %d\n",evernota[0].n);  
        printf("o meu int é %d\n",evernota[20].n); 

        return 0; 
      } 

6 Comments

Okay thanks guys, but look I am getting segmentation fault, why??
I have the headers, and I did as you said.. struct nota{ int n; char text[30]; char titulo[100]; char login[20]; }
error: incompatible types when returning type 'int' but 'struct nota' was expected
Aha! A missing semicolon in the struct definition. Try again!
you mean after the struct definition? I had struct note{ blabla blabla } i put the ; after the last } , but I keep getting segmentation fault.. :s thanks alot mate.. any solution?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.