Useful macro...or dangerous??
Dave Jones
djones at megatest.UUCP
Thu Apr 28 14:40:01 AEST 1988
in article <221 at raunvis.UUCP>, kjartan at raunvis.UUCP (Kjartan Pier Emilsson Jardedlisfraedi) says:
>
...
> As I quickly became tired of gigantic IF tests, I decided
> to build some sort of equality macro, and came down on the following
> solution which seems to work.
...
>
> #define EQ(A,B) equal(A,B,sizeof(*(A)))
>
...
> I herewith submit my possible blunders to the Blowtorches of the Net.
>
> Kjartan Pierre Emilsson, Reykjavik, ICELAND
I admire your courage. No torch here, so relax.
As Dustin Hoffman said in Marathon Man, "It's not safe."
Due to packing alignment within the struct, there
may be some garbage bits "in the cracks". You could get a "not equal"
result when in fact, the structures should compare as equal.
If you use this technique, be sure that you bzero() structures when you
allocate them. Don't be too suprised if you, or somebody else, forgets to
do so, and the world, as we know it, comes to an end.
Also, use the C-library function bcmp() rather than your equal().
Good luck,
Dave J.
More information about the Comp.lang.c
mailing list