Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!brl-adm!seismo!rochester!cornell!batcomputer!braner From: braner@batcomputer.UUCP Newsgroups: comp.lang.c Subject: Re: Auto variable with sizeof == 0 Message-ID: <159@batcomputer.tn.cornell.edu> Date: Wed, 11-Feb-87 01:15:47 EST Article-I.D.: batcompu.159 Posted: Wed Feb 11 01:15:47 1987 Date-Received: Thu, 12-Feb-87 00:02:05 EST References: <4114@brl-adm.ARPA> <4053@utcsri.UUCP> Reply-To: braner@batcomputer.UUCP (braner) Organization: Theory Center, Cornell University, Ithaca NY Lines: 37 Summary: How about structure elements with size 0? [] 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); where length is as needed at the time for that line. The actual text of the line is stored OUTSIDE the struct, starting at lineptr->text[0]. This is, of course, "illegal". Some compilers give a warning about "zero-size structure element". Question: Do some compilers refuse to accept this? Is there a GOOD way to do it legally? (NOTE: I KNOW that you can use: ... char *text; ... lineptr = malloc(sizeof(LINE)); lineptr->text = malloc(length); - but the illegal version saves the overhead of the extra pointer and the overhead of the extra malloc() control block. In this application this saving is important, since there will be hundreds or even thousands of LINEs.) - Moshe Braner