`open ended' struct declarations (was Re: question on opendir, readdir, etc.)

Martin Weitzel martin at mwtech.UUCP
Tue Dec 4 02:24:20 AEST 1990


In article <14616 at smoke.brl.mil> gwyn at smoke.brl.mil (Doug Gwyn) writes:
>In article <4776 at rossignol.Princeton.EDU> tr at samadams.princeton.edu (Tom Reingold) writes:

>>How can it work?  And why was this kludge put there?
>
>What do you mean, how can it work?  If you look at the sources you
>said you have, you should see that there is actually more storage
>present than one might guess from just looking at the struct dirent.

But this *is* confusing to many who are accustomed to use a given
struct declaration `as is' to define a variable.

A similar problem comes up when SysV messages are used. Here is the
relevant excerpt from /usr/include/sys/msg.h

-------------------------------------------------------------------------
/*
**	User message buffer template for msgsnd and msgrecv system calls.
*/

struct msgbuf {
	long	mtype;		/* message type */
	char	mtext[1];		/* message text */
};
--------------------------------------------------------------------------

I've seen it more than once that the word "template" was overlooked
in the above comment and a variable is defined as

	struct msgbuf buffer;

as msgrcv() has (among others) one argument of type `struct msgbuf *'
and `&buffer' is handed to msgrcv(). Even the fact that msgrvc() requires
an extra argument for the maximum message length is not enough warning.
(Somehow they seem to trust that some "magic" will get it right - but
*if* this works, it is by accident not by magic).

I think the problem is that the #include-file specifies a syntactically
correct struct-declaration, which must be used in very unusual way if
it comes to a definition, e.g.

	struct msgbuf *bp = malloc(offsetof(struct msgbuf, mtext[0])
					+ LENGTH_I_NEED_THIS_TIME);
-- 
Martin Weitzel, email: martin at mwtech.UUCP, voice: 49-(0)6151-6 56 83



More information about the Comp.lang.c mailing list