pointer returned by shmat points at the end of address space which gives seg fault

纵然是瞬间 提交于 2020-01-16 03:30:12

问题


 //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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!