Zero Length Arrays Allowed in C Standard?

P E Smee exspes at gdr.bath.ac.uk
Mon Dec 18 22:22:47 AEST 1989


In article <1989Dec14.182113.5398 at twwells.com> bill at twwells.com (T. William Wells) writes:
>In article <1989Dec12.110042.29290 at gdt.bath.ac.uk> exspes at gdr.bath.ac.uk (P E Smee) writes:
>The above is missing some casts. And it can be done as well with

Well, I said I'd simplified it.  

Saving the pointers, zeroing the whole thing, and then putting the
pointers back I considered, but the construct occurs in a
frequently-used part of the program, so I was willing to sacrifice
beauty for speed.  I don't recall why I didn't substructure the stuff
after the pointers, but I do recall thinking at the time that it would
not be a great idea in context.  I *have* localized the definition of
my clear operation so that you only have to understand what I'm doing
(and why) once in the program.

>On the other hand, it has the advantage that it will likely
>generate slightly better code than any of the other methods. the
>first two methods, for example, are very likely to generate
>several extra instructions to compute the amount of space to
>clear.

I was trusting the compiler to realize that the size of the structure was
known at compile time (though not to me), and so to optimize that
computation.  My trust was repaid on the one machine where I looked at
the object code generated.

>Warning: null pointers and floating point zeros are *not*
>necessarily represented by bit patterns of all zero bits.

True, well known, and not an issue in my case.  (In fact, on all my
target machines, they are; but I've learned not to count on that.)

I also contemplated (since the my struct was fixed size) putting the
pointers at the end, and using one of them as the effective marker.
However, that interacted badly with the paging algorithm on one of my
target machines (extra paging to get at the most frequently used item)
and didn't gain any compensating advantage on any of the others.

I suppose I ought to say that the construct is *NOT* one that I am
particularly proud of, and I don't find it beautiful, but it *IS* a
taken-from-life example of where a zero-length entity could sensibly be
used.  (Which is all I meant it to be.)  It is also well-commented in
my original to indicate what I am doing, why I am doing it that way,
and why I am not happy that I have done.

-- 
Paul Smee, Univ of Bristol Comp Centre, Bristol BS8 1TW, Tel +44 272 303132
 Smee at bristol.ac.uk  :-)  (..!uunet!ukc!gdr.bath.ac.uk!exspes if you MUST)



More information about the Comp.lang.c mailing list