Auto variable with sizeof == 0

mikes at apple.UUCP mikes at apple.UUCP
Thu Feb 19 05:57:35 AEST 1987


We got bit by this one, where the user does
struct Line {
	some overhead fields, like links, etc.
	char data[1]; /* actually, can be a lot more than one */
};
	The idea was that the data part is at the end if struct Line,
and can be VERY long.
	We were using a Green Hills C compiler, which had this nice
feature of using 'short math' for certain array index calculations.
	Of course, when the 'data' got to be VERY long, short math
won't do, and this caused some hard-to-find problems.
	Personally, I would incur the overhead of having an extra
pointer in the structure, but if you really want to allocate the
data *as part of struct Line*, then I am left with the feeling
that the proper way to do this is:
struct Line {
	overhead fields
	char data[MAX_IT_CAN_EVER_BE];
};
	and allocate it via
	malloc(sizeof(struct Line) - MAX_IT_CAN_EVER_BE + sizeofyourdata)

	This isn't super clean, but I expect that for a language without
dynamic arrays.
-- 
			Michael Shannon {apple!mikes}



More information about the Comp.lang.c mailing list