Pointer returned by shmat points at the end of the address space, which gives a seg fault
//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 problems with this segment of code. The Word class is a class that I defined with an 8 byte char array and some parsing functions. I think I use shmget and shmat in the same way that others use them. But I get rejected all the time. When I print the Seg_id it only looks ok with some number. But Seg_ptr points to 0xffffffffffffffff. Then the next line of code is clearly giving me an error. I want to know why Seg_ptr is pointing to end of memory space. Thanks in advance!
source to share
After testing, it seems that the non-root user cannot use shmat or it will return permission.
last update: (SHM_KEY, sizeof (...), (IPC_CREAT | 0666)). But I kept getting an invalid argument with this; it turns out that the shared memory segment already exists. Then I could use ipcs to check if I already have a shared memory segment with the same key and use ipcrm to free it.
source to share