Data alignment requirements can make this interpretation difficult. Multi-field keys may contain gaps or padding between fields that don't align properly otherwise. For example, on some system architectures the struct
contains a three-byte pad betweenstruct S { char c; int i; };
c
and i
. The contents of pads are
undefined, which means that a byte-by-byte comparison of what should be
identical instances of struct S
may fail due to differences in the bits
stored in the pad.
The problem this causes for hashing is clear: if the hash function includes the pad bytes as part its computation,struct S s1 = { .c = 'a', .i = 10 }, s2 = { .c = 'a', .i = 10 }; assert(0 == memcmp(&s1, &s2, sizeof(struct S))) // may fail.
s1
and s2
could produce
different hash values, violating the requirement that identical keys produce
identical hash values.
This page last modified on 24 January 2006.