0
votes

I am getting myself familiar with the linked list and dynamic memory.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
    char *name;  // name of student
    char ID[7];  // student ID (nul terminated)
    // you may add fields to this structure if needed
} Student;

typedef struct Course_st {
    // add fields here
    Student *Student_list;
    struct Course * next;
} Course;


// kirjoita ohjelma tähän

int main()
{
    Course *course_1 = (Course*)malloc(1*sizeof(Course));

    Student *st_1 =malloc(sizeof(Student));
    st_1->name = malloc(5*sizeof(char));
    strcpy(st_1->name,"Hien");
    strcpy(st_1->ID,"001");

    course_1->Student_list = st_1;
    course_1->next = malloc(sizeof(Course));

    Student *st_2 = malloc(4*sizeof(Student));
    st_2->name = malloc(4*sizeof(char));
    strcpy(st_2->name,"Kim");
    strcpy(st_2->ID,"002");
    
    Course* next = (Course*) course_1->next;
    next->Student_list = st_2;
    next->next= NULL;

    while(course_1 != NULL)
    {
        printf("%s %s\n", course_1->Student_list->name, course_1->Student_list->ID);
        free(course_1->Student_list->name);
        free(course_1->Student_list);
        course_1 = (Course*)course_1->next;
    }
    free(next);
}

and I got back this error...

in use at exit: 16 bytes in 1 blocks

total heap usage: 7 allocs, 6 frees, 4,217 bytes allocated

16 bytes in 1 blocks are definitely lost in loss record 1 of 1

at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

by 0x1086EB: main (teht3.c:21)

LEAK SUMMARY:

definitely lost: 16 bytes in 1 blocks

...

For counts of detected and suppressed errors, rerun with: -v

ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

2
The program allocated something on line 21, and it never freed it.user253751

2 Answers

1
votes

Everything you allocate with malloc(...) should also be released with free(...). In your case, you do not free course_1. So the solution should be:

Course * tmp = course_1;
Course * tmp_next;    

while(tmp != NULL)
{
    printf("%s %s\n", tmp->Student_list->name, tmp->Student_list->ID);
    free(tmp->Student_list->name);
    free(tmp->Student_list);
    tmp_next = tmp->next;
    free(tmp);
    tmp = tmp_next;
}
0
votes

I would change the end of your program like this

        Course *old_course_1=course_1;
        course_1 = (Course*)course_1->next;
        free(old_course_1);
    }
    // free(next);

This way, you free course_1 as soon as you consider the next one; thus the last call to free(next) is not necessary.

You were correctly freeing the dynamic parts of course_1 but not course_1 itself.