2

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

6
  • 1
    How do you know the shmat worked "fine"? You did not do error checking... and array_id is used uninitialized. Never post modified and incomplete code, always post complete and actual code we can compile! Commented Nov 9, 2012 at 4:46
  • @Jens Yes he did, he is checking that error is = -1. Im confused about the perror(array detachment) part, is that actually a thing in c? Commented Nov 9, 2012 at 4:49
  • sorry i left the error checking out but i did check after attaching. i know it worked fine because i didn't get any errors and i also assigned some strings and printed them out. and i've been told to use perror() instead of printf() O.o Commented Nov 9, 2012 at 4:51
  • @Ben No he didn't. I'm talking about the attach, not the detach. Detaching when the attach already failed is no surprise. Commented Nov 9, 2012 at 4:51
  • 1
    @Tim This is impossible to diagnose unless you provide a complete, self contained program we can compile. So far all we have is "Doctor, it hurts when I do something." :-) Commented Nov 9, 2012 at 4:53

2 Answers 2

1

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");
}
Sign up to request clarification or add additional context in comments.

3 Comments

There is no need to cast records[0] when test for ==(void*)-1).
@alk :I saw the man page for shmat in that its given (void*) -1..That's why i wrote. Can you elaborate a little for me here because comparison of pointer to integer will give you wanrning won't it?
records[0] is a pointer (a char *). Any pointer can be assigned and comparted to void* without casting. This casting -1 to void* is just a dirty trick to have shmat() be able to return some error.
1

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.

2 Comments

My answer implies that the problem does not lie in how you attach/detach. The code is basically Ok. If you want to polish it up go for Omkant's answer. @Tim
you were right, i didn't modify records[0] and the error went away. @alk

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.