struct comparison

nevin.j.liber nevin1 at cbnewsc.ATT.COM
Fri Jul 28 12:57:21 AEST 1989


In article <2410 at trantor.harris-atd.com> bbadger at x102c.harris-atd.com (Badger BA 64810) writes:

>When I entered this discussion, it was centering on whether struct comparison
>was a well-defined and useful operation.  I think that has been established
>for structs not containing unions.  Whether to add it to the language --
>in particular whether to include it in ANSI C -- is another, more 
>(mostly?) political question.  

It is NOT mostly a political question!  There are problems that would
be introduced with it!  Consider the following scenario:

Jane Programmer, fresh out of college, reads all about C with the
struct equality extension (C-SEE :-)), and is raring to go program
with it.  She, along with her colleages, write 10,000,000 lines of code
in C-SEE.  An example of a struct used in this code (and there are
thousands) is:

	struct typical
	{
		int	foo;
			/* ... hundreds of variables */
		char	bar;
	}

Three years later, you come on the scene.  Your boss asks you to make
some enhancements to the ten million line program, and one of them requires
you to change struct typical (among other things).  Now it looks like:

	struct typical
	{
		int	foo;
			/* ... hundreds of variables */
		char	bar;
		short	uh_oh;
	}

Guess what happens?  The code breaks!!  By seemingly innocently adding
a field to the struct, you now have to go through and fix all the code
you broke.  If you are lucky, and you remembered to do this before you
sent the code to the testers, you could get the compiler to find all
the instances of the struct (by simply adding a union to the struct
typical definition, and having the compiler give you the error lines),
but that isn't the point.

Also, how do you plan on fixing the code?  Are you going to think about
each and every struct comparison, and decide on-the-spot whether or not
the rewrite it the way you would in standard C (if a union was added to
typical, you would *have* to rewrite ALL the C-SEE dependent stuff)?


The only situation where the C-SEE extension would be useful is when
you know that you just want to compare all the elements of the struct
together.  In order to do this, you can't abstract over the struct; you
have to *know* what each element is in order to use C-SEE safely.  It
is better self-documentation, as well as safer, to just write out the
member comparisions longhand.
-- 
NEVIN ":-)" LIBER  AT&T Bell Laboratories  nevin1 at ihlpb.ATT.COM  (312) 979-4751



More information about the Comp.std.c mailing list