structure and array and string comparisons
ShanklandJA
jas at druxy.UUCP
Sat Mar 24 02:40:25 AEST 1984
For goodness' sake! When you need to do structure or array
comparison, how hard is it to write a little macro that does
the job? Under most circumstances, I can't see it taking more
than 5 minutes. (Yes, people sometimes use structures with
dozens -- or hundreds of fields. But how often? How often do
such structures need to be compared in their entirety? Is it worth
hacking up the language to accomodate such cases?
The advantages of the macro approach are:
(1) you get to interpret "equality" any way you want: dictionary
order for some fields, numerical equality for others, etc.;
pointers must point to one and the same object to be equal,
or pointers to equal-valued objects are considered equal
(and of course, you get to decide what makes the pointed-to
objects "equal");
(2) you stop creeping featurism in its tracks.
One objection I foresee is that people will say that the compiler
could generate more efficient comparison code directly, say by
emitting a single block-compare instruction to compare several
truly adjacent fields ("truly adjacent" means no gap for alignment),
whereas if you compare each field separately in a macro, the compiler
will most likely emit separate compare instructions for each field.
But I suggest the solution to that is to use a compiler that will optimize
such code sequences into a single block-compare wherever possible.
Granted, I'm not at all sure that a C compiler that optimizes to
that extent exists anywhere, on any machine; but just wait....
Keep C small!
Jim Shankland
..!ihnp4!druxy!jas
More information about the Comp.lang.c
mailing list