简体   繁体   English

结构上的 memcmp 与 c lang 中的 integer 变量的比较如何。 结果不如预期

[英]How the memcmp on structure with integer variable in c lang compares. Result is not as expected

I have a structure with integer, I am comparing the struct by using memcmp, I don't want to use other memthods.我有一个 integer 的结构,我正在使用 memcmp 比较结构,我不想使用其他方法。

   #include <stdio.h>
   #include <string.h>

   typedef struct Foo 
   {
      int d;
   } Foo;

   int main(int argc, const char * argv[])
   {
      Foo one, two;
      int result;
   
      memset(&one,0,sizeof(Foo));
      memset(&two,0,sizeof(Foo));
    
      one.d = 1022;
      two.d = 1024;
    
      result = memcmp((void*)&one, (void*)&two, sizeof(Foo));
      printf("comp with assignment %d\n",result);
    
      if (result == 0) printf("Arrays are the same\n");
    
      return 0;
   }

memcmpa should return -1, but it returns 1. why? memcmpa 应该返回 -1,但它返回 1。为什么? memcmp woth one.d = 1022 and two.d = 1023 will return correct value. memcmp one.d = 1022 和 two.d = 1023 将返回正确的值。 why is so?为什么会这样?

If you add two printf s in your code:如果在代码中添加两个printf

   typedef struct Foo 
   {
      int d;
   } Foo;

   int main(int argc, const char * argv[])
   {
      Foo one, two;
      int result;
   
      memset(&one,0,sizeof(Foo));
      memset(&two,0,sizeof(Foo));
    
      one.d = 1022;
      two.d = 1024;

      printf("%04x %04x\n", one.d, two.d);
    
      result = memcmp((void*)&one, (void*)&two, sizeof(one));
      printf("comp with assignment %d\n",result);
    
      if (result == 0) printf("Arrays are the same\n");
    
      return 0;
   }

Result:结果:

03fe 0400
comp with assignment 1

You will see that the first byte of one is 0xfe and the first byte of two is 0x00 (they are in opposite order as most modern machines are little endioan) So 0xfe > 0x00 and memcmp returns 1您会看到一个字节的第one字节是0xfe ,而two字节的第一个字节是0x00 (它们的顺序相反,因为大多数现代机器都是小字节序的)所以0xfe > 0x00并且memcmp返回1

It compares bytes , not int s:它比较bytes ,而不是int s:

memcmp - This function reads object representations, not the object values, and is typically meaningful for byte arrays only: struct s may have padding bytes whose values are indeterminate, memcmp - This function reads object representations, not the object values, and is typically meaningful for byte arrays only: struct s may have padding bytes whose values are indeterminate,

Look at how an int looks at byte level and you'll see it more clearly.看看一个int是如何看待字节级别的,你会看得更清楚。 It can be stored with the most significant byte first or last - and the result of memcmp will depend on that.它可以与最重要的字节首先最后存储 - 并且memcmp的结果将取决于此。

You can create your own memcmp_debug for this purpose.为此,您可以创建自己的memcmp_debug

Example:例子:

int memcmp_debug(const void *vpa, const void *vpb, size_t len) {
    const unsigned char *a = vpa, *b = vpb;

    puts("comparing these:");
    for(size_t i = 0; i < len; ++i) {
        printf("%2d %02X %02X\n", i, a[i], b[i]);
    }

    puts("\ncomparing:");
    for(unsigned i = 0; i < len; ++i) {
        int result = (int)a[i] - (int)b[i];
        printf("%2d %02X %02X  =>  %d\n", i, a[i], b[i], result);
        if(result) return result;
    }
    return 0;
}

Possible output:可能的 output:

comparing these:
 0 FE 00
 1 03 04
 2 00 00
 3 00 00

comparing:
 0 FE 00  =>  254

.. and here it returned on the first byte compared (the least significant byte on my machine) and returned a positive value just like it did for you. ..在这里它返回比较的第一个字节(我机器上的最低有效字节)并返回一个正值,就像它为你所做的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM