[comp.lang.c] `open ended' struct declarations

martin@mwtech.UUCP (Martin Weitzel) (12/03/90)

In article <14616@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes:
>In article <4776@rossignol.Princeton.EDU> tr@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@mwtech.UUCP, voice: 49-(0)6151-6 56 83

jap@convex.cl.msu.edu (Joe Porkka) (12/05/90)

martin@mwtech.UUCP (Martin Weitzel) writes:

>In article <14616@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes:
>>In article <4776@rossignol.Princeton.EDU> tr@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

>But this *is* confusing to many who are accustomed to use a given

>(Somehow they seem to trust that some "magic" will get it right - but
>*if* this works, it is by accident not by magic).

Not so much magic since they wrote the compiler.....

Pehaps it should be viewed as a defiency in the C language.
If we can functions  foo(arg1, arg2, ...)
why can't we have structs
	struct my_struct {
		struct  somedata sd;
		int length;
		char moredata[...]; /* Even better moredata[length] */
	}
then of course more syntax is needed to actually declare objects
of type my_struct which includes the exact size.