Re: simple question about the function memcmp in kernel

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Apr 8, 2013 at 5:33 AM,  <Valdis.Kletnieks@xxxxxx> 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

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@xxxxxxxxxxxxxxxxx
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies




[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux