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?
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_twould be an appropriate type.