问题
//TeamSize is an integer
int Seg_id = shmget(SHM_KEY,sizeof(Word)*TeamSize,IPC_CREAT);
void* Seg_ptr = shmat(Seg_id,0,0);
new(Seg_ptr) Word[TeamSize];
I am having trouble with this segment of code. The Word class is a class that I defined with 8 bytes char array and some parse functions. I think I am using shmget and shmat just like how others use them. But I keep getting seg fault. When I print out Seg_id, it looks normal just some number. But Seg_ptr points at 0xffffffffffffffff. Then the next line of code obviously gives me seg fault. I want to know why Seg_ptr points at the end of memory space. Thanks in advance!
回答1:
After testing, it seems that non-root user cannot use shmat or it will return Permission Denied.
late update: setting permission helps, such as shmget(SHM_KEY, sizeof(...),(IPC_CREAT | 0666)).But I kept getting invalid argument with this ; it turned out shared memory segment with the same key already existed. I could then use ipcs to check if I already have the shared memory segment with the same key and use ipcrm to free it.
来源:https://stackoverflow.com/questions/19075589/pointer-returned-by-shmat-points-at-the-end-of-address-space-which-gives-seg-fau