Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!rutgers!cbmvax!vu-vlsi!colin From: colin@vu-vlsi.UUCP Newsgroups: comp.lang.c Subject: Re: Auto variable with sizeof == 0 Message-ID: <626@vu-vlsi.UUCP> Date: Fri, 13-Feb-87 11:24:07 EST Article-I.D.: vu-vlsi.626 Posted: Fri Feb 13 11:24:07 1987 Date-Received: Sat, 14-Feb-87 02:14:20 EST References: <4114@brl-adm.ARPA> <4053@utcsri.UUCP> <159@batcomputer.tn.cornell.edu> Reply-To: colin@vu-vlsi.UUCP (Colin Kelley) Organization: Villanova Univ. EE Dept. Lines: 33 Summary: Another solution to the malloc() case In article <159@batcomputer.tn.cornell.edu> braner@batcomputer.UUCP (braner) writes: > >In the famous "microEMACS" by David Conroy, which has been widely >utilized and modified, the basic text-line structure looks like this: > >typedef struct LINE { > struct LINE *nextline; > struct LINE *prevline; > short size; /* s.b. int! */ > short used; > char text[]; /* !!!!!!!!! */ >} LINE; > >The idea is to allocate memory for lines as follows: > > lineptr = malloc(sizeof(LINE)+length); Some other people suggested declaring the text field to be char *text, but I'm surprised no one suggested this: Declare the text field to be char text[1], then use lineptr = malloc(sizeof(LINE)-1+length); Almost all compilers will optimize sizeof(LINE)-1 into a single constant, so the code generated is likely to be exactly the same as that generated for the uEmacs example above...[Of course you can cast the argument to (unsigned) to keep lint happy.] Gnuplot (which we posted a couple weeks ago) uses this technique because it seemed to be the most portable... -Colin Kelley ..{cbmvax,pyrnj,bpa}!vu-vlsi!colin