What Can't C Do?

rcd at opus.UUCP rcd at opus.UUCP
Fri Mar 16 12:37:28 AEST 1984


<>
 > While you're on the subject, what about == to compare two structures?
 > This is even more reasonable, in that structure assignment is supported...
Yet another bad idea.  This is much harder than it looks, for at least the
following reasons:

First, because of alignment constraints, structures may contain "holes" -
space allocated but not used.  Consider a 16-bit machine with a word
alignment constraint on ints and
	struct {int a; char b; int c;} . . .
In order to get both a and c aligned correctly, there's a one-byte hole
after b (assuming typical allocation of fields).  If the compiler is to
generate code to compare two instances of these structs, it has to skip
over the holes to avoid a spurious inequality result.  (Remember that even
though external variables can be initialized by the compiler - holes and
all - automatic variables may not be and usually aren't because of cost.)

A variant of the same problem occurs if the structure contains a union.
There is no way to know which part of a union is currently in effect, so
there's no way to know how much should be compared if the elements of the
union do not have the same length.
-- 
{hao,ucbvax,allegra}!nbires!rcd



More information about the Comp.lang.c mailing list