Path: utzoo!mnetor!uunet!swlabs!teemc!rolls!mtuxo!homxb!ho7cad!ekb From: ekb@ho7cad.ATT.COM (Eric K. Bustad) Newsgroups: comp.lang.c Subject: Re: Useful macro...or dangerous?? Message-ID: <322@ho7cad.ATT.COM> Date: 28 Apr 88 18:02:06 GMT References: <221@raunvis.UUCP> Organization: AT&T Bell Laboratories, Holmdel, NJ Lines: 43 Keywords: macro Summary: not safe In article <221@raunvis.UUCP>, kjartan@raunvis.UUCP (Kjartan Pier Emilsson Jardedlisfraedi) asks: > [ If the following is a foolproof way to test for structure equality. ] > > #define EQ(A,B) equal(A,B,sizeof(*(A))) > > equal(a,b,size) > char *a,*b; > long int size; > { > while(*(a+si-1)== *(b+si-1) && si>0) > si--; > if(si==0) > return(1); > else > return(0); > } The main problem with this is that there are often "holes" in the structure that should be ignored when checking for equality. For example, on the machine I'm on now, the structure struct gub { int a; char b; }; takes *eight* bytes of memory, four for the int, one for the char and three more to pad it out to a multiple of eight bytes. The structure struct hvc { char b; int a; }; also takes eight bytes, with the extra three being inserted before the int to place it at the proper alignment. These holes could contain almost any kind of random garbage, so two structures which compared equal field-wise may not when each byte is compared in your equal() function. = Eric