简体   繁体   English

C:结构数组上的 qsort() -- 返回垃圾

[英]C: qsort() on struct array -- garbage is returned

I'd like to sort a struct array with the qsort() library function.我想使用qsort()库 function 对结构数组进行排序。 I've read man 3 qsort thoroughly, and consulted other sources on the internet, however, with no luck.我已经彻底阅读了man 3 qsort ,并查阅了互联网上的其他资源,但是没有运气。

I thought I had made sure that the nmemb and size arguments are correct.我以为我已经确保nmembsize arguments 是正确的。

I've also ensured that the issue didn't arise from my surrounding code, and rather comes from my lack of understanding of qsort() , by writing the following example:通过编写以下示例,我还确保问题不是由我周围的代码引起的,而是来自我对qsort()缺乏理解:

#include <stdio.h>
#include <stdlib.h>

typedef struct example_structure {
    size_t a;
    size_t b;
} example_structure;

static int cmp_example_structure(const void *h1, const void *h2) {
    return (((example_structure *)h1)->a - ((example_structure *)h2)->a);
}

int main() {
    example_structure *example_structures = calloc(10, sizeof(example_structure));

    printf("sizeof(example_structures[0]) = %lu; sizeof(example_structure *) = %lu\n", sizeof(example_structures[0]), sizeof(example_structure *));

    fputc('\n', stdout);

    for(size_t h = 0; h < 10; h++) {
        example_structures[h].a = h;
        example_structures[h].b = h;
    }

    for(size_t h = 0; h < 10; h++) {
        printf("%ld ", example_structures[h].b);
    }

    fputc('\n', stdout);

    for(size_t h = 0; h < 10; h++) {
        printf("0x%lX ", example_structures[h].b);
    }

    fputc('\n', stdout);
    fputc('\n', stdout);

    qsort(example_structures, sizeof(example_structures[0]), 10, cmp_example_structure);

    for(size_t h = 0; h < 10; h++) {
        printf("%ld ", example_structures[h].b);
    }

    fputc('\n', stdout);

    for(size_t h = 0; h < 10; h++) {
        printf("0x%lX ", example_structures[h].b);
    }

    fputc('\n', stdout);

    free(example_structures);
}

I've noticed that the returned garbage appears to be shifted:我注意到返回的垃圾似乎被转移了:

0 1 2 3 4 5 6 7 8 9 
0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 

0 1 17179869184 6 34359738368 131072 1970346311811072 131072 1970324836974592 9 
0x0 0x1 0x400000000 0x6 0x800000000 0x20000 0x7000500000000 0x20000 0x7000000000000 0x9 

How may I resolve this issue?我该如何解决这个问题?

Thanks in advance.提前致谢。

qsort(3): sort array - Linux man page says: qsort(3): 排序数组 - Linux 手册页说:

 void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));

The qsort() function sorts an array with nmemb elements of size size. qsort() function 对具有大小为大小的 nmemb 元素的数组进行排序。 The base argument points to the start of the array. base 参数指向数组的开头。

As you see, you should specify the number of elements as the 2nd argument and the size of one element as the 3rd argument.如您所见,您应该将元素的数量指定为第二个参数,将一个元素的大小指定为第三个参数。

In other words,换句话说,

qsort(example_structures, sizeof(example_structures[0]), 10, cmp_example_structure);

should be应该

qsort(example_structures, 10, sizeof(example_structures[0]), cmp_example_structure);

You have the size and nmemb arguments to qsort backwards.你有sizenmemb arguments 向后qsort You're telling it you're trying to sort sizeof(example_structures[0]) elements each of size 10, not 10 elements each of size sizeof(example_structures[0]) .您告诉它您正在尝试对sizeof(example_structures[0])每个大小为 10 的元素进行排序,而不是对每个大小为sizeof(example_structures[0])的 10 个元素进行排序。

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

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