Sys V IPC: My final word

Doug Gwyn gwyn at brl-smoke.ARPA
Sun Feb 16 13:39:27 AEST 1986


In article <2666 at gatech.CSNET> hope at gatech.CSNET (Theodore Hope) writes:
>
>The man pages for msgop(2) describe the msgbuf struct as containing
>
>	long mtype;     /* message type */
>	char mtext [];  /* message text */
>
>This kind of struc, especially in a syscall-passing sense, seems odd to me.
>It is obvious that mtext should be
>
>	char mtext [some_number];
>
>"Oh," said I.  "I'll bet that's a misprint.  Let's look at the <sys/msg.h>
>file to see what they _really_ mean."  Well, surprise.  The .h file defines
>
>	struct msgbuf {
>	   long mtype;
>	   char mtext [1];	<- Notice: it says [1]
>	}
>
>Am I overlooking something obvious?  After looking through the kernel source,
>it appeared that the msgsnd and msgrcv syscalls expect the data to start at 
>msg.mtext, so by defining my own structs as
>
>	struct Msgbuf {
>	   long mtype;
>	   char mtext [MAXMTEXT];
>	}
>
>everything is ok.

You got it right.  The C language provides no way to declare
a variable-length array in a struct, so the prototype in the
documentation and the declaration in the header file represent
the array as best they can.  From a design standpoint it would
have been preferable for the struct to have contained a
pointer to the message buffer rather than the buffer itself,
but that's not the way they designed it.



More information about the Comp.unix mailing list