문제

I have:

int array_id;
char* records[10];

// get the shared segment
if ((array_id = shmget(IPC_PRIVATE, 1, 0666)) == -1) {
            perror("Array Creating");
}

// attach
records[0] = (char*) shmat(array_id, (void*)0, 0);
if ((int) *records == -1) {
     perror("Array Attachment");
}

which works fine, but when i try and detach i get an "invalid argument" error.

// detach
int error;
if( (error = shmdt((void*) records[0])) == -1) {
      perror(array detachment);   
}

any ideas? thank you

도움이 되었습니까?

해결책

In shmdt(), no need to convert the pointer argument to void* it will automatically take care of this .

Remove (void*) from shmdt((void*) records[0])). It should be like this.

if ((error = shmdt(records[0]) ) == -1)
{
  perror("Array detachment");
}

And it will work.

Also in shmat() , on error it returns (void*) -1 so your comparison will give warning. so do like this

if ((char *)records[0] == (void *)-1)
{
  perror("Array Attachment");
}

다른 팁

Assuming attaching went well, invalid argument simply means that either the segment has already been detached, or the value of records[0] had changed since it was set by attaching.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top