Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!uunet!mcsun!unido!mikros!mwtech!martin From: martin@mwtech.UUCP (Martin Weitzel) Newsgroups: comp.lang.c Subject: `open ended' struct declarations (was Re: question on opendir, readdir, etc.) Message-ID: <988@mwtech.UUCP> Date: 3 Dec 90 15:24:20 GMT References: <4767@rossignol.Princeton.EDU> <14611@smoke.brl.mil> <4776@rossignol.Princeton.EDU> <14616@smoke.brl.mil> Reply-To: martin@mwtech.UUCP (Martin Weitzel) Organization: MIKROS Systemware, Darmstadt/W-Germany Lines: 45 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 Brought to you by Super Global Mega Corp .com