Saturday, May 22, 2010

How to compare structure in C language? Whether it is compiler dependent?

This questions dependent on C language....

How to compare structure in C language? Whether it is compiler dependent?
The previous answer is not only wrong, it is


inefficiently wrong :-)





With respect to the efficient part, you do NOT


need to write a "compare" function as described


because it already exists in all ANSI C


implementations. See the function "memcmp()" in


your documentation.





With respect to correctness, this solution won't


consistently work. What's really terrible is


that it does work *some* of the time.





Consider this struct:





struct foo {


    char a;


    long count;


}





While field "a" will be at offset "0" within the


struct, on many machines "count" will be at


offset "4" (with 3 bytes of padding between


them). It will be as if you declared the struct


as:





struct foo {


    char a;


    char padding[3];


    long count;


}





If you create the struct as a stack variable or


from a call to "malloc", it can initially have


garbage in those padding bytes. Then if you


assign a value to "a" and "count", you still have


garbage in the padding area. Two structs with


identical values for "a" and "count" will still


compare unequal if they have different garbage in


the padding area.





Thus, the "memcmp" strategy for comparing them


won't consistently work if there is any padding


in the struct. In this case, you are forced to


compare fields.
Reply:Not to "pile on", but it may be worth pointing


out that the alignment padding isn't the only


source of bugs in the solution suggested by


"Vasudev TS".





A similar problem can be caused by bit fields


which don't completely fill up the word that


they're in:





typedef struct {


int id:12;


int group:12


int flag:1


} key_t;





struct x {


key_t key;


struct x *next;


}





In this simple example, there are 7 unused bits


in the "key_t" bitfield, which can vary between


otherwise identical copies of "struct x".





Even if you carefully check that your structure


doesn't contain any alignment padding or unused


bitfield space, how can you protect against


someone making a change (ie. adding another field)


some time in the future? What's insidious is


that it might not fail during your regression


tests, but only on some particular data set


while running your customer's data.





Bottom line: Bitwise struct comparisons don't work.
Reply:If you are comparing two structures of the same type, you can do so by writing a c function that takes te two structures and the size as arguments and compare them as if they are characters. To make it clearer the function could be some thing like this:





int compare(char *a, char *b, size)





This function compares both a and b and returns 1 if same and 0 if not.





while calling it from your main you could call it as below:





struct str *s1, *s2;


//assign values to the members of both ....





sts=compare ((char *)s1, (char *)s2, sizeof(struct str));





Additional Details:


I have answered a similar question earlier and provided a tested program as an example: Please refer to the question titled:





"How can we check whether the contents of two structure variables are same or not?"





in yahoo answers under programming %26amp; design section


No comments:

Post a Comment