0

我正在制作一个函数来获取每个大小为 SIZ 的 NMEMB 成员数组的最大值,并将每个成员与memcmp(). 问题是在比较有符号整数时,结果是不正确的,但同时是正确的。这是一个例子:

void *
getmax(const void *data, size_t nmemb, size_t siz){

    const uint8_t *bytes = (const uint8_t *)data;
    void *max = malloc(siz);

    if (!max){
        errno = ENOMEM;
        return NULL;
    }
    
    memcpy(max, bytes, siz);
    while (nmemb > 0){

        hexdump(bytes, siz);

        if (memcmp(max, bytes, siz) < 0)
            memcpy(max, bytes, siz);

        bytes += siz;
        --nmemb;
    }

    return max;
}

int
main(int argc, char **argv){
    int v[] = {5, 1, 3, 1, 34, 198, -12, -11, -0x111118};
    size_t nmemb = sizeof(v)/sizeof(v[0]);
    int *maximum = getmax(v, nmemb, sizeof(v[0]));
    printf("%d\n", *maximum);
    return 0;
}
  • hexdump()只是一个调试功能,不会改变程序。

编译执行时输出如下:

05 00 00 00 // hexdump() output
01 00 00 00
03 00 00 00
01 00 00 00
22 00 00 00
c6 00 00 00
f4 ff ff ff
f5 ff ff ff
e8 ee ee ff
-11  // "maximum" value

这是正确的,因为memcmp()比较一个字节字符串并且不关心类型或符号,所以-11 = 0xfffffff5数组中的最大字节字符串是,v[]但同时不正确,因为-11它不是数组中的最大整数。

有没有办法使用这个函数获取数组的最大整数?

4

3 回答 3

3

memcmp compares the locations and does not care about the sign. so for it -11 means 0xFFFFFFF5 and -12 means 0xFFFFFFF4 and the biggest number in the array 198 means 0x000000C6, so out of all these, -11 is the biggest unsigned number and it is returned for you. You should not use memcmp to compare the signed numbers.

于 2021-02-27T16:21:16.950 回答
2

Go down the qsort route and require a custom comparator. Note that you absolutely don't need dynamic memory allocation in a function this simple:

#include <stdio.h>

void const *getmax(void const *data, size_t const count, size_t const elm_sz,
                   int (*cmp)(void const *, void const *)) {
  char const *begin = data;
  char const *end = begin + count * elm_sz;
  char const *max = begin;
  while (begin != end) {
    if (cmp(max, begin) < 0) max = begin;
    begin += elm_sz;
  }

  return max;
}

int int_cmp(void const *e1, void const *e2) {
  int const i1 = *(int const *)e1;
  int const i2 = *(int const *)e2;

  if (i1 > i2) return 1;
  if (i1 < i2) return -1;
  return 0;
}

int main() {
  int v[] = {5, 1, 3, 1, 34, 198, -12, -11, -0x111118};
  int const *maximum = getmax(v, sizeof(v) / sizeof(*v), sizeof(*v), int_cmp);
  printf("%d\n", *maximum);
}
于 2021-02-27T16:16:37.977 回答
1

All memory comparisons made by memcmp are unsigned and based on char sized array elements. When you feed this with a signed int array of cells, different size, your result can only be used to test equality of binary representations, meaning that a result of 0 or different than 0 means equality or unequality, but the sign on a different of zero result means comparing the individual bytes of the array of integeres, which, descomposed as bytes (in the machine endianness architecture), makes some of the bytes to be signed and compared as unsigned and others be signed and compared as unsigned. In addition, the significance of the different bytes in an integer will probably affect the sorting order, as the bytes are compared from lower addresses to higher addresses, that would match with the architecture endianness only in the case that the integers where stored as unsigned and (very important) stored in memory in big endian order. If probably you are using intel architecture, then this is just the opposite to be able to use that.

于 2021-02-27T16:30:31.593 回答