Paul Eggert wrote:
> The 'char' type can have padding bits, which means that code like this:
> 
>      char const *needle = (char const *) needle_start;
>      char const *haystack = (char const *) haystack_start;
>      if ((unsigned char) *needle == (unsigned char) *haystack) ...
> 
> might ignore some of the bits in the storage addressed by A and B.
> To get a proper memcmp-style comparison one must do something like this:
> 
>      unsigned char const *needle = (unsigned char const *) needle_start;
>      unsigned char const *haystack = (unsigned char const *) haystack_start;
>      if (*needle == *haystack) ...
> 
> (In the latter version, the casts are needed only because this code is
> intended to be portable to C++.)
I disagree. Citing ISO C 99, section 7.21.4:

   "The sign of a nonzero value returned by the comparison functions
    memcmp, strcmp, and strncmp is determined by the sign of the
    difference between the values of the first pair of characters
    (both interpreted as unsigned char) that differ in the objects
    being compared."

It says that the _values_ of the characters are interpreted as
unsigned char. I.e. like this:

    if ((unsigned char) *needle == (unsigned char) *haystack) ...

It does _not_ say that the memory storage should be interpreted as
'unsigned char[]'.

Bruno



Reply via email to