struct comparison

Guy Harris guy at auspex.auspex.com
Thu Jul 20 09:10:09 AEST 1989


>Sure, you _may_ run into special cases, but is this any reason to leave out 
>a useful operation?

Well, it depends on how useful it *really* is; given enough "special
cases", it may be that the only cases where it *is* useful are special
cases, in which case you can whip up a macro to do the comparison.

>Other languages, such as Ada (Oh no! The A-word!), provide for comparison
>of composite objects (records in Ada correspond to structs in C).

So how does Ada define comparison of records?

>Of course there should be caveats about padding data being compared.

I.e., "padding data gets compared"?  That doesn't sound like all that
useful an operation to me; in order to be useful, I'd have to zero out
structures on e.g. the stack.

>Also unions should only be compared to another union or object
>which is *actually using* the same representation.  (That is, given:
>	union lf_t {long l; float f} lf_a, lf_b;
>	lf_a.l = 10; 
>	lf_b.f = 10.0;
>we should probably not be testing (lf_a == lf_b), but rather 
>(lf_a.l == (long) lf_b.f).)

Fine, so how is the compiler to know to do that?  C unions are not
discriminated unions; there's nothing in the union itself to indicate
which member is being used.

>However, the fact remains that either simple ``compare all bytes'' or 
>an expansion to compare all struct members recursively, would be a simple
>extension which could be quite useful.

Simple "compare all bytes" is certainly simple; how useful it is is
another question, given that comparing padding bytes is simply wrong,
unless you *guarantee* that they're always going to have some "standard"
value that gets in the way.  You still haven't indicated how unions are
to be compared, so an expansion to compare all struct members
recursively would fail if any such members were unions.  And, once
again, I'll point out that pointer comparison may or may not be what you
want, either.

In other words, comparison might be "simple" in a restricted sense, but
nobody's proven that in that "simple" sense (i.e., bitwise comparison
either of all the bits in the structure, or only the "meaningful" bits)
this would be anywhere near "quite useful".

If you want abstract data types, you know where to find them....



More information about the Comp.std.c mailing list