Zero Length Arrays Allowed in C Standard?

Wm E Davidsen Jr davidsen at crdos1.crd.ge.COM
Fri Dec 8 03:43:33 AEST 1989


In article <12468 at ulysses.homer.nj.att.com> gsf at ulysses.homer.nj.att.com (Glenn Fowler[drew]) writes:

| why not make the last element huge rather than small and then at malloc time
| decrease the sizeof rather than increase:
| 
|	[  example  ]
|
| or is this just as sleazy as the undersized array example?

  It has one advantage over the use of size zero or one: if you have a
compiler which checks subscripts (I haven't seen one, but Sabre might)
this will allow subscripts up to the max range.

  The advantage of the zero size array is that then the size of the
malloc is (sizeof(struct whatever) + datasize), while in the case of the
big declaration its (sizeof(struct whatever) - (maxsize - datasize)).
The former is easier to write and understand, although I'd bury the
whole thing in a macro called with just the size.

#define getstructx(size) \
  (struct whatever *)malloc(sizeof(struct whatever)+size)
-- 
bill davidsen	(davidsen at crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon



More information about the Comp.lang.c mailing list