简体   繁体   中英

Memory Leak in my C program

The following code is working, but I am getting a memory leak. The program is using linked list and dynamic memory function. The Program excepts a .txt file as a command line argument.

Example file:

Jacob Miller 832323244 21
Tom Darren 8931701830 19
Paul Mitchell 329797231 20
Here the output from 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)

There is some problem here code snippet.

    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_);

    }

I am freeing the above through this method

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

Here is my full code

 #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);
    }

    }

This code does not initialize tmp :

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

Consequently, you have undefined behavior when reading from an uninitialized variable.

You probably meant to use head != NULL as your while condition.

freeNodeList ,您仅释放学生记录节点,而不释放它们包含的学生记录,这意味着所有记录仍在程序末尾分配,尽管由于您破坏了列表而无法访问它们。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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