内存泄漏在我的C程序中

时间:2017-04-05 23:55:05

标签: c memory-leaks linked-list

以下代码正常运行,但我收到内存泄漏。 该程序使用链表和动态记忆功能。 该程序将.txt文件作为命令行参数除外。

示例文件:

  

Jacob Miller 832323244 21
  Tom Darren 8931701830 19
  Paul Mitchell 329797231 20
  这里是Valgrind的输出

==5742== HEAP SUMMARY:
==5742==     in use at exit: 324 bytes in 9 blocks
==5742==   total heap usage: 11 allocs, 2 frees, 908 bytes allocated
==5742== 
==5742== 52 bytes in 1 blocks are definitely lost in loss record 2 of 4
==5742==    at 0x4A0610C: malloc (vg_replace_malloc.c:195)
==5742==    by 0x400A2C: student_record_allocate (new.c:106)
==5742==    by 0x4007EB: main (new.c:46)
==5742== 
==5742== 272 (16 direct, 256 indirect) bytes in 1 blocks are definitely lost in loss record 4 of 4
==5742==    at 0x4A0610C: malloc (vg_replace_malloc.c:195)
==5742==    by 0x400A1E: student_record_allocate (new.c:104)
==5742==    by 0x4008D1: parseFile (new.c:74)
==5742==    by 0x400803: main (new.c:47)
==5742== 
==5742== LEAK SUMMARY:
==5742==    definitely lost: 68 bytes in 2 blocks
==5742==    indirectly lost: 256 bytes in 7 blocks
==5742==      possibly lost: 0 bytes in 0 blocks
==5742==    still reachable: 0 bytes in 0 blocks
==5742==         suppressed: 0 bytes in 0 blocks
==5742== 
==5742== For counts of detected and suppressed errors, rerun with: -v
==5742== ERROR SUMMARY: 66 errors from 21 contexts (suppressed: 4 from 4)
  

此处的代码段存在一些问题。

    void parseFile(char* filename, struct student_record_node** head)
    {
    FILE *fp;
    student_record_node *sn;

    fp = fopen(filename, "r");
    if (!fp) {
        fprintf(stderr, "Can't Open file %s\n", filename);
        exit(EXIT_FAILURE);
    }

    while(!feof(fp)) {
        sn = student_record_allocate();

        fscanf(fp, "%20s", sn->record_->first_name_);
        fscanf(fp, "%20s", sn->record_->last_name_);
        fscanf(fp, "%d", &(sn->record_->student_id_));
        fscanf(fp, "%d", &(sn->record_->student_age_));

        appendNode(*head, sn);
    }

    fclose(fp);
    }

    void printNode(struct student_record_node* node)
    {
        printf("\nstruct student_record_node:\n");
        if (node) {
        printf("\tstudent first name: %s\n",node->record_->first_name_);
        printf("\tstudent second name: %s\n",node->record_->last_name_);
        printf("\tstudent id: %d\n", node->record_->student_id_);
        printf("\tstudent age: %d", node->record_->student_age_);

    }
    }

    struct student_record_node* student_record_allocate()
    {
    student_record_node *newNode=NULL;
    student_record *newRecord=NULL;

    newNode = (student_record_node *)malloc(sizeof(student_record_node));
    newRecord = (student_record *)malloc(sizeof(student_record));
    newNode->record_ = newRecord;
    newNode->next_ = NULL;

    return newNode;
    free(newNode->record_);
    }    

    {
        free(node->record_);
    free(node->next_);

    }

我通过这种方法释放了上述内容

    void freeNodeList(struct student_record_node* head)
    {
    student_record_node *tmp;
     while(tmp != NULL) {
     tmp=head;
     head=head->next_;   
     free(tmp);
    }
    head=NULL;
    }
  

这是我的完整代码

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

    struct student_record
    {
    int student_id_;
    int student_age_;
    char first_name_[21];
    char last_name_[21];
    }s;

    struct student_record_node
    {    
    struct student_record* record_;
    struct student_record_node* next_;
    };
    typedef struct student_record student_record;
    typedef struct student_record_node student_record_node;

    void parseFile(char* filename, struct student_record_node** head);
    void printNode(struct student_record_node* node);
    struct student_record_node* student_record_allocate();
    void student_record_node_deallocate(struct student_record_node* node);
    void sortByAge(struct student_record_node** recordsHead);
    void sortById(struct student_record_node** recordsHead);
    void swap(struct student_record_node** node1, struct student_record_node** node2);
    void freeNodeList(struct student_record_node* head);
    void appendNode(struct student_record_node* head, struct student_record_node* newNode);
    void printNodeList(struct student_record_node* head);
    int main(int argc, char **argv)
    {
    student_record_node *head;
    if (argc != 2) {
        fprintf(stderr, "Error: expected input file");
        exit(EXIT_FAILURE);
    }
    /*  student_record *newRecord=NULL;
    recordsHead->record_ = newRecord;
    recordsHead->next_ = NULL;
    */
    head = student_record_allocate();
    parseFile(argv[1], &head);
    printf("Before sorting...");
    printNodeList(head);
    printf("Sorting by age...");
    sortByAge(&head);
    printNodeList(head);
    printf("Sorting by id...");
    sortById(&head);
    printNodeList(head); 
    freeNodeList(head);
    free(head);

    return 0;
    }

    void parseFile(char* filename, struct student_record_node** head)
    {
    FILE *fp;
    student_record_node *sn;

    fp = fopen(filename, "r");
    if (!fp) {
        fprintf(stderr, "Can't Open file %s\n", filename);
        exit(EXIT_FAILURE);
    }

    while(!feof(fp)) {
        sn = student_record_allocate();

        fscanf(fp, "%20s", sn->record_->first_name_);
        fscanf(fp, "%20s", sn->record_->last_name_);
        fscanf(fp, "%d", &(sn->record_->student_id_));
        fscanf(fp, "%d", &(sn->record_->student_age_));

        appendNode(*head, sn);
    }

    fclose(fp);
    }

    void printNode(struct student_record_node* node)
    {
        printf("\nstruct student_record_node:\n");
        if (node) {
        printf("\tstudent first name: %s\n",node->record_->first_name_);
        printf("\tstudent second name: %s\n",node->record_->last_name_);
        printf("\tstudent id: %d\n", node->record_->student_id_);
        printf("\tstudent age: %d", node->record_->student_age_);

    }
    }

    struct student_record_node* student_record_allocate()
    {
    student_record_node *newNode=NULL;
    student_record *newRecord=NULL;

    newNode = (student_record_node *)malloc(sizeof(student_record_node));
    newRecord = (student_record *)malloc(sizeof(student_record));
    newNode->record_ = newRecord;
    newNode->next_ = NULL;

    return newNode;
    free(newNode->record_);
    }    

    {
        free(node->record_);
    free(node->next_);

    }

    void sortByAge(struct student_record_node **recordsHead)
    {
    int swapped, i;
    struct student_record_node *ptr1;
    struct student_record_node *lptr = NULL;

    do
    {
        swapped = 0;
        ptr1 = *recordsHead;

        while (ptr1->next_ != lptr)
        {
            if (ptr1->record_->student_age_ > ptr1->next_->record_->student_age_)
            {
                swap(&ptr1, &ptr1->next_);
                swapped = 1;
            }
            ptr1 = ptr1->next_;
        }
        lptr = ptr1;
    }
    while (swapped);
    }

    void sortById(struct student_record_node** recordsHead)
    {
    int swapped, i;
    struct student_record_node *ptr1;
    struct student_record_node *lptr = NULL;

    do
    {
        swapped = 0;
        ptr1 = *recordsHead;

        while (ptr1->next_ != lptr)
        {
            if (ptr1->record_->student_id_ > ptr1->next_->record_->student_id_)
            {
                swap(&ptr1, &ptr1->next_);
                swapped = 1;
            }
            ptr1 = ptr1->next_;
        }
        lptr = ptr1;
    }
    while (swapped);
    }

    void swap(struct student_record_node** node1, struct student_record_node** node2)
    {
    student_record_node *tmp, *n1, *n2;

    n1 = *node1;
    n2 = *node2;
    tmp->record_= n1->record_;
    n1->record_= n2->record_;
    n2->record_ = tmp->record_;

    }

    void freeNodeList(struct student_record_node* head)
    {
    student_record_node *tmp;
     while(tmp != NULL) {
     tmp=head;
     head=head->next_;   
     free(tmp);
    }
    head=NULL;
    }

    void appendNode(struct student_record_node* head, struct 
    student_record_node* newNode)
    {    
    student_record_node *prev, *next;

    if(head == NULL) {
        head = newNode;
        return;
    }

    next = head;
    while(next != NULL) {
        prev = next;
        next = next->next_;
    }
    prev->next_ = newNode;
    }

    void printNodeList(struct student_record_node* head)
    {
    student_record_node *tmp = head;

    while(tmp != NULL) {
        tmp = tmp->next_;
        printNode(tmp);
    }

    }

2 个答案:

答案 0 :(得分:1)

此代码未初始化tmp

void freeNodeList(struct student_record_node* head) {
    student_record_node *tmp;
    while(tmp != NULL) {
        tmp=head;
        head=head->next_;   
        free(tmp);
    }
    head=NULL;
}

因此,从未初始化的变量中读取时,您有未定义的行为。

您可能打算将head != NULL用作while条件。

答案 1 :(得分:0)

freeNodeList中,您只释放学生记录节点,而不是它们包含的学生记录,这意味着所有记录仍然在您的程序结束时分配,尽管它们是因为您销毁了它们而无法访问列表。