简体   繁体   中英

c++ memory leak on my data structure

So I've graduated from learning C and now learning C++. From what I understand C and C++ are similar in variety of ways so I'm trying to re-write my past C project files into C++. However I keep getting a memory leak when I try to print out my information. Can someone tell me why I am getting a memory leak in my code.

struct.h

typedef struct student_info {
    char last[10];
    char first[10];
    int student_id;
    int count_student;
} student;

typedef struct course_info {
    char name[10];
    int course_id;
    int count_course;
    student *students;
} course;

typedef struct gradebook_info {
    course *courses;
} gradebook;

function.c

void new_course(gradebook *info) {
    int i, loop=0;

    cout << "Enter Number of Courses " ;
    cin >> loop;

    for(i=0; i<loop; i++) {

        cout << "Enter Course ID ";
        cin >> info->courses[info->courses->count_course].course_id;

        cout << "Enter Course Name ";
        cin >> info->courses[info->courses->count_course].name;

        info->courses->count_course++;

    }
}

void new_student(gradebook *info) {
    int i, loop=0;

    cout << "Enter Number of Students " ;
    cin >> loop;

    for(i=0; i<loop; i++) {

        cout << "Enter Student ID ";
        cin >> info->courses->students[info->courses->students->count_student].student_id;

        cout << "Enter Last Name ";
        cin >> info->courses->students[info->courses->students->count_student].last;

        cout << "Enter First Name ";
        cin >> info->courses->students[info->courses->students->count_student].first;

        info->courses->students->count_student++;
    }
}

void printCourse(gradebook *info) {
    int i;

    cout << "Course ID\tCourse Name\t" << endl;

    for(i=0; i<info->courses->count_course; i++) {

        cout << info->courses[i].course_id << "\t\t";
        cout << info->courses[i].name << endl;
    }
}

void printStudent(gradebook *info) {
    int i; 

    cout << "Student ID\tLast Name\tFirst Name\t" << endl;

    for(i=0; i<info->courses->students->count_student; i++) {

        cout << info->courses->students[i].student_id << "\t\t";
        cout << info->courses->students[i].last << "\t\t";
        cout << info->courses->students[i].first << endl;
    }
}
  • When I run new_course() function it works.
  • I run my printCourse() function it works.
  • I run new_student() function it works.
  • When I run printStudent() function it works.
  • Then I try to run printCourse() function again and when i=2 , I get some information from my struct student.

I can't figure it out. Any help is appreciated.

main.c

gradebook *info = new gradebook; //allocate memory

do {
    main_menu();

    int option=0;

    switch(option) {
        case 1: new_course(info);
            break;
        case 2: new_student(info);
            break;
        case 3: printCourse(info);
            break;
        case 4: printStudent(info);
            break;
    }
}while(option < 13);

delete(info);

You have not only to allocate memory for the gradebook , but also for the courses and the students . For example:

gradebook *info = new gradebook;
info->courses = new course_info[10];
for(int i = 0; i < 10; i++)
    info->courses[i]->students = new student_info[10];

Beware, this example limits the number of courses and students per course to 10.

PS: Your code might work for one or two iterations if you don't allocate memory for the courses and the students . But in this case you screw up your memory.

You need to allocate the pointers students and course using new .

After getting loop:

students = new student_info* [loop] and

courses= new course_info* [loop]

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