[英]Sorting an array of struct pointers
我有一个函数可以按字母顺序对结构指针数组进行排序,如下所示:
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
分配会泄漏在循环之前分配的内存。 我认为您根本不需要在sort函数中分配内存或释放内存。 实际上,我确定您不会; 您可以在循环后释放存储在insertItem
的最后一个项目,然后在以后访问(并释放)已经释放的内存时出现问题。
具有分配的单个整数的数据结构非常浪费(尤其是在64位计算机上); 它应该是结构的普通int
成员。 如果要处理固定大小的名称,则也可以将名称放入固定大小的数组中。 但是,这是一个较小的改进(未实现)。 整数指针不便于将candidate
结构用于内部数据。 我本可以使用&(int){ 8000 }
这样的C99'复合文字',但这需要更多说明(并且数组不再是static
,必须修改对new_candidate()
的调用)。
您还需要将while
循环中的限制从k > 0
更改为k >= 0
; 否则,亨利埃塔(Henrietta)排在榜首。
这是一个或多或少的最小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;
}
输出示例:
Sorted Alphabetically
Adrian = 1000 votes
Beatrice = 2000 votes
Charles = 3000 votes
Diana = 4000 votes
Eric = 5000 votes
Francesca = 6000 votes
George = 7000 votes
Henrietta = 8000 votes
Ian = 9000 votes
Janice = 10000 votes
祝贺Janice赢得选举。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.