Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!hp4nl!phigate!prle!prles2!cstw68!meulenbr From: meulenbr@cstw68.prl.philips.nl (Frans Meulenbroeks) Newsgroups: comp.std.c Subject: Re: variable-length struct hack Message-ID: <809@prles2.UUCP> Date: 8 Dec 89 13:41:32 GMT References: <448@longway.TIC.COM> <450@longway.TIC.COM> <15364@haddock.ima.isc.com> Sender: nobody@prles2.UUCP Reply-To: meulenbr@cstw68.prl.philips.nl (Frans Meulenbroeks) Organization: Centre for Software Technology, Philips Eindhoven Lines: 24 In article <15364@haddock.ima.isc.com> karl@haddock.ima.isc.com (Karl Heuer) writes: [about dirent.h & the form of a dirent struct] >I question this. It seems to me that > typedef struct { junk_t xx; char name[1]; } T; > T *p = (T *)malloc(sizeof(T) + strlen(s)); > strcpy(p->name, s); >is legal and portable, and I believe I can rigorously prove it from the rule >"objects are composed of bytes": [proof deleted; see original article] Karl, I like your proof. However, a struct definition like that does not allow indexing in the name array under implementations which check for array out of bounds. (by the way, does ANSI allow index out of bound checks? Are they forbidden? Is it left to the implementor? I could not find anything in the draft) E.g.: if s = "Hello World", I'd like p->name[3] to result in an 'l', and not in a syntax error. I for me, I would prefer char *name instead of char name[1]. Frans Meulenbroeks (meulenbr@cst.prl.philips.nl) Centre for Software Technology ( or try: ...!mcvax!phigate!prle!cst!meulenbr)