foo.text[0] Was: Auto variable with sizeof == 0

meissner at dg_rtp.UUCP meissner at dg_rtp.UUCP
Fri Feb 27 22:42:58 AEST 1987


In article <4157 at utcsri.UUCP> greg at utcsri.UUCP (Gregory Smith) writes:
>In article <159 at batcomputer.tn.cornell.edu> braner at batcomputer.UUCP (braner) writes:
> >
> >typedef struct LINE {
> >	struct LINE *nextline;
> >	struct LINE *prevline;
> >	short       size;		/* s.b. int! */
> >	short       used;
> >	char        text[];		/* !!!!!!!!! */
> >}	LINE;
> 
> .... the offset of 'text' within the structure is *not*
> equal to sizeof(LINE)-1, so the above call to malloc is asking for N
> bytes too many, where N is 3 on a vax and 1 on a 68K or PDP-11.
> 
> The problem is that the struct will be padded out after the one-byte
> 'text' array to meet alignment requirements for the pointer fields.
> 'sizeof(LINE)' includes this padding. 

	yea, and malloc will also add anywhere of 0-64 extra bytes in the
memory allocation.  Extreme micro optimizations like this can backfire.

(by micro optimizations, I don't mean optimizations for memory limited
micro's, but extreme efforts here and there to save a byte or a milli-
second) while ignoring the more stubstanal gains to be made by looking
at the program as a whole for improvements.
-- 
	Michael Meissner, Data General	Uucp: ...mcnc!rti-sel!dg_rtp!meissner

It is 11pm, do you know what your sendmail and uucico are doing?



More information about the Comp.lang.c mailing list