C 链表实现内存分配

C 链表实现内存分配,c,pointers,struct,linked-list,C,Pointers,Struct,Linked List,我使用C中的结构和指针实现了一个链表,如下所示: #include <stdio.h> #include <stdlib.h> #include <string.h> /* these arrays are just used to give the parameters to 'insert', * to create the 'people' array */ #define HOW_MANY 7 char * names[HOW_MANY] = {

我使用C中的结构和指针实现了一个链表,如下所示:

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

/* these arrays are just used to give the parameters to 'insert',
 * to create the 'people' array */

#define HOW_MANY 7
char * names[HOW_MANY] = {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim", "Harriet"};
int ages[HOW_MANY] = {22, 24, 106, 6 ,18, 32, 24};

/* declare your struct for a person here */
struct p {
    char *peopleName;
    int age;
    struct p* next;
} person;


struct p* insert_end (struct p *people,char *name, int age) {
    /* put name and age into nthe enxt free place in the array parameter here */
    /* modify nextfreeplace here */
    struct p *nextPerson;

    nextPerson = (struct p*)malloc(sizeof(struct p));

    if (nextPerson == NULL) {
        printf("Error performing malloc.\n");
        return(NULL);
    } else {
        nextPerson -> peopleName = name;
        nextPerson -> age = age;
        if (people == NULL) {
            nextPerson -> next = people;
            return nextPerson;
        } else {
            struct p* temp = people;
            while ((temp -> next) != NULL) {
                temp = temp -> next;
            }
            temp -> next = nextPerson;
            nextPerson -> next = NULL;
            return people;
        }
    }
}



int main (int argc, char **argv) {
    /* declare the people array here */

    struct p *people = NULL;

    for (int i = 0; i < HOW_MANY; i++) {
        people= insert_end(people, names[i], ages[i]);
    }

    while (people != NULL) {
        printf("Freeing\n");
        free(people);
        people = people -> next;
    }


    return 0;
}
struct p* insert_end (struct p *people,char *name, int age) {
    /* put name and age into nthe enxt free place in the array parameter here */
    /* modify nextfreeplace here */
    struct p *nextPerson;

    nextPerson = (struct p*)malloc(sizeof(struct p));

    if (nextPerson == NULL) {
        printf("Error performing malloc.\n");
        return(NULL);
    } else {
        nextPerson -> peopleName = name;
        nextPerson -> age = age;
        if (people == NULL) {
            nextPerson -> next = people;
            return nextPerson;
        } else {
            while ((people -> next) != NULL) {
                people= people-> next;
            }
            people-> next = nextPerson;
            nextPerson -> next = NULL;
            return people;
        }
    }
}

如您所见,当使用此实现时,内存没有被正确释放。有谁能帮助我理解为什么这不起作用吗?

这个函数显然应该返回指向列表中第一个条目的指针。第二个实现不会返回指针,而是在列表上至少有一个条目的情况下返回指向下一个到最后一个条目的指针。

不是问题的答案,但即使使用运行良好的第一个版本的
insert\u end
,这段代码中仍有未定义的行为:

while (people != NULL) {
    printf("Freeing\n");
    free(people);
    people = people -> next;  // << you are using people after it has been freed
}

你没有真正解释问题所在。@MichaelWalz不,我没有t@AkshaiShah:忘记那个评论,错误的复制粘贴,我的错。我会在几分钟内删除这些评论。顺便说一句:你的算法效率很低,因为每次插入都需要遍历列表中的所有元素才能找到最后一个。您应该维护指向最后一个元素的指针。
while (people != NULL) {
    printf("Freeing\n");

    struct p *temp = people ; 
    people = people -> next;
    free(temp);
}