Auto variable with sizeof == 0
braner at batcomputer.UUCP
braner at batcomputer.UUCP
Wed Feb 11 16:15:47 AEST 1987
[]
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
More information about the Comp.lang.c
mailing list