simple question about the function memcmp in kernel
Max Filippov
jcmvbkbc at gmail.com
Sun Apr 7 21:56:29 EDT 2013
On Mon, Apr 8, 2013 at 5:33 AM, <Valdis.Kletnieks at vt.edu> wrote:
> On Mon, 08 Apr 2013 08:57:01 +0800, Ben Wu said:
>
>> int memcmp(const void *cs, const void *ct, size_t count)
>> {
>
>> I want to know why it use the temp pointer su1, su2? why it doesn't directly
>> use the cs and ct pointer?
>
> This is a C 101 question, not a kernel question. But anyhow..
>
> They're declared const, so the compiler will whine about ++'ing them.
const is the the object they point to, not the pointers themselves
(that would be
void * const cs).
memcmp compares bytes at which cs and ct point, but these are void pointers,
and the expression res = *cs - *ct is thus meaningless. One must convert them
to (const unsigned char *), which looks ugly, otherwise such implementation
looks like pretty much valid:
int memcmp(const void *cs, const void *ct, size_t count)
{
int res = 0;
for (; 0 < count; ++cs, ++ct, count--)
if ((res =*(const unsigned char *)cs - *(const unsigned char
*)ct) != 0)
break;
return res;
}
--
Thanks.
-- Max
More information about the Kernelnewbies
mailing list