The following is the implementation of memcmp in Microsoft CRT:
int memcmp(const void* buf1,
const void* buf2,
size_t count)
{
if(!count)
return(0);
while(--count && *(char*)buf1 == *(char*)buf2 ) {
buf1 = (char*)buf1 + 1;
buf2 = (char*)buf2 + 1;
}
return(*((unsigned char*)buf1) - *((unsigned char*)buf2));
}
It basically does a comparison of bytes by bytes.
My question has two parts:
- Is there any reason not to change this for int by comparing int to
count < sizeof(int)and then compare bytes by bytes for what remains? - If I did 1, are there any potential / obvious issues?
Notes: I do not use CRT at all, so I have to implement this function anyway. I'm just looking for advice on how to implement it correctly.
source
share