C 对结构指针数组进行排序

C 对结构指针数组进行排序,c,arrays,pointers,struct,C,Arrays,Pointers,Struct,我有一个函数可以按字母顺序对结构指针数组进行排序,如下所示: void insertionSortAlpha(candidate* person[], int lo, int hi) { candidate* insertItem; insertItem = malloc(sizeof(candidate)); insertItem->name = malloc(25*sizeof(char)); insertItem-&

我有一个函数可以按字母顺序对结构指针数组进行排序,如下所示:

 void insertionSortAlpha(candidate* person[], int lo, int hi) { 
        candidate* insertItem;
        insertItem = malloc(sizeof(candidate));
        insertItem->name = malloc(25*sizeof(char));
        insertItem->numVotes=malloc(sizeof(int));   

        int i;
        for(i=lo+1; i<=hi; i++) { 
            insertItem = person[i];
            int k = i - 1;
            while (k > 0 && strcmp(insertItem->name, person[k]->name) < 0) { 
                person[k + 1] = person[k];
                --k;
            }
            person[k + 1] = insertItem;
        }
    free(insertItem->numVotes);
    free(insertItem->name);
    free(insertItem);}
然后我调用函数并像这样释放内存。这些名称是从文件中填充的。打印时,它总是跳过数组中第三个元素的名称和编号。它还给我一个无效的指针错误。为什么会这样

insertionSortAlpha(Person, 0, 9);
  printf("\n\n");
  printf("Sorted Alphabetically\n");
  for (i=0; i<10;i++) {
    printf("%s =  %d votes \n", Person[i]->name, *Person[i]->numVotes);
  }
 for (i =0; i<10; i++) { 
    free(Person[i]->numVotes);
    free(Person[i]->name);
    free(Person[i]);
  }

创建MCVE需要做的工作比应该做的多一点,但这段代码基本上验证了我的:

将insertItem赋值到循环中会泄漏循环之前分配的内存。我认为排序函数中根本不需要内存分配或释放。事实上,我相信你不会;在循环之后释放insertItem中存储的最后一项,这将在以后访问和释放已释放的内存时给您带来问题

分配了单个整数的数据结构非常浪费,尤其是在64位机器上;它应该是结构的普通int成员。如果要处理固定大小的名称,也可以将名称放入固定大小的数组中。然而,这只是一个小的改进,没有实现。整数指针使内部数据使用候选结构变得不方便。我本可以使用C99‘复合文字’,比如&int{8000},但这需要更多的解释,数组不能再是静态的,必须修改对新的_候选者的调用

您还需要将while循环中的限制从k>0更改为k>=0;否则,亨丽埃塔将排在榜首

这是一个或多或少最小的MCVE,修复到位:

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

typedef struct candidate
{
    char *name;
    int  *numVotes;
} candidate;

static
void insertionSortAlpha(candidate *person[], int lo, int hi)
{
    candidate *insertItem;
    //insertItem = malloc(sizeof(candidate));
    //insertItem->name = malloc(25 * sizeof(char));
    //insertItem->numVotes = malloc(sizeof(int));

    int i;
    for (i = lo + 1; i <= hi; i++)
    {
        insertItem = person[i];
        int k = i - 1;
        // k > 0 --> k >= 0
        while (k >= 0 && strcmp(insertItem->name, person[k]->name) < 0)
        {
            person[k + 1] = person[k];
            --k;
        }
        person[k + 1] = insertItem;
    }
    //free(insertItem->numVotes);
    //free(insertItem->name);
    //free(insertItem);
}

typedef struct BaseData
{
    char *name;
    int   numVotes;
} BaseData;

static void *emalloc(size_t size, const char *func)
{
    void *vp = malloc(size);
    if (vp == 0)
    {
        fprintf(stderr, "Failed to allocate %zu bytes of memory in %s()\n", size, func);
        exit(EXIT_FAILURE);
    }
    return vp;
}

static candidate *new_candidate(const char *name, int votes)
{
    candidate *person = emalloc(sizeof(*person), __func__);
    person->name = emalloc(25, __func__);
    assert(strlen(name) < 25);
    strcpy(person->name, name);
    person->numVotes = emalloc(sizeof(int), __func__);
    *person->numVotes = votes;
    return person;
}

int main(void)
{
    candidate *Person[10];
    static const BaseData people[] =
    {
        { "Henrietta",  8000 },
        { "Eric",       5000 },
        { "Beatrice",   2000 },
        { "Diana",      4000 },
        { "Francesca",  6000 },
        { "George",     7000 },
        { "Ian",        9000 },
        { "Janice",    10000 },
        { "Adrian",     1000 },
        { "Charles",    3000 },
    };

    for (int i = 0; i < 10; i++)
        Person[i] = new_candidate(people[i].name, people[i].numVotes);

    insertionSortAlpha(Person, 0, 9);
    printf("\n\n");
    printf("Sorted Alphabetically\n");
    for (int i = 0; i < 10; i++)
    {
        printf("%-15s = %6d votes\n", Person[i]->name, *Person[i]->numVotes);
    }

    for (int i = 0; i < 10; i++)
    {
        free(Person[i]->numVotes);
        free(Person[i]->name);
        free(Person[i]);
    }

    return 0;
}

祝贺Janice赢得选举。

您在循环中插入项目的任务泄漏了循环前分配的内存。我认为排序函数中根本不需要内存分配或释放。事实上,我相信你不会;循环结束后,释放insertItem中存储的最后一项,这会在以后访问和释放已释放的内存时给您带来问题。顺便说一句,您的文件看起来像什么?