1

I have two structures :

struct bets{
   int bets[36];
   int last_bet;
}bets
struct board{
   int type;
   bets *bet;
 }board

I created a chunk of shad memory of sizeof(board). So, I got a pointer to the Board in shared memory (lest call it ptr). I did created a new board and bets structure, board *b, bets * bts, .... added board->bet = bts. Now, I copied the "b" to ptr memcpy(ptr, bts, sizeof(board)). I can access the ptr->type. But when I try to access ptr->bet->last_bet, I get a segmentation-fault error.

I also tried copying like this:

board *b;
memcpy(ptr, b, sizeof(board));
bets *bts;
memcpy(ptr->bet, bts, sizeof(bets)).

Still getting segmentation-fault error.

How can I copy both struct one inside of the other and still have access to the nested one?

eeerahul
  • 1,629
  • 4
  • 27
  • 38
Onica Radu
  • 183
  • 2
  • 4
  • 11
  • 1
    Will you piece together [all of](http://stackoverflow.com/questions/8073704/c-structure-in-shared-memory) your [homework](http://stackoverflow.com/questions/8077884/accesing-shm-structure-using-pointer) via SO questions? :-) – Kerrek SB Nov 10 '11 at 13:19
  • i still cant manage to do anything... :(( everytime that i think im getting somewhere i get another error im sorry – Onica Radu Nov 10 '11 at 13:21

1 Answers1

8

Standard "deep copying" into shared memory is not useful because the pointers, even if they point into the shared memory segment, are local to your process's virtual address space and won't be the same when another process maps the shared memory. Instead of pointers, you need to store offsets from the beginning of the shared memory segment. size_t would be an appropriate type.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711