I am submitting my school workshop through matrix and I am getting this warning about memory leak, however, when I compile it and run through the g++ command or VS, it does not show any warnings or errors.
That is the beginning of the submission
Checking due date:
ON TIME.
Compiling:
g++ -Wall -std=c++11 -o ws utils.o School.cpp schoolTester.cpp Subject.cpp utils.cpp 2> errors.txt
Compile result:
Success! no errors or warnings...
Testing execution:
READ THE FOLLOWING CAREFULLY!
I am about to execute the tester and capture the output in "student_output.txt"
Please enter the values carefuly and exactly as instructed.
Press <ENTER> to start...
Script started, file is student_output.txt
==203824== Memcheck, a memory error detector
==203824== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==203824== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==203824== Command: ws
==203824==
Begin Testing the Program!
--------------------------
Please enter the name of the school:
>
When I ran the code I get this summary
------------------------
End Testing the Program!
==209192==
==209192== HEAP SUMMARY:
==209192== in use at exit: 200 bytes in 1 blocks
==209192== total heap usage: 9 allocs, 8 frees, 366 bytes allocated
==209192==
==209192== LEAK SUMMARY:
==209192== definitely lost: 200 bytes in 1 blocks
==209192== indirectly lost: 0 bytes in 0 blocks
==209192== possibly lost: 0 bytes in 0 blocks
==209192== still reachable: 0 bytes in 0 blocks
==209192== suppressed: 0 bytes in 0 blocks
==209192== Rerun with --leak-check=full to see details of leaked memory
==209192==
==209192== For counts of detected and suppressed errors, rerun with: -v
==209192== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Script done, file is student_output.txt
Here is the code itself:
School.cpp
namespace sdds {
School Sch;
Subject Sub[5];
void read(School& Sch) {
Sch.m_name = new char[30];
char name [61];
cout << "Please enter the name of the school:\n> ";
read( name, 61, "Name is too long, only 60 characters allowed!\nRedo Entry: ");
strcpy(Sch.m_name, name);
cout << "Please enter the number of subjects offered by " << Sch.m_name << ": ";
Sch.m_noOfSubjects = new int[50];
read(*Sch.m_noOfSubjects, 2, 50, "Invalid Number of subjects, 2<=ENTRY<=50\nRedo Entry: ");
Sch.m_subjects = new int[*Sch.m_noOfSubjects];
for (int i = 0; i < *Sch.m_noOfSubjects; i++) {
cout << i+1 << ") ------------------------------" << endl;
read(Sub[i]);
}
}
int report(const School&Sch) {
int totalEnr = 0;
cout << Sch.m_name<<endl;
cout << "Subject Enrollments" << endl;
for (int i = 0; i < *Sch.m_noOfSubjects; i++) {
totalEnr += report(Sub[i]);
}
cout << "Total enrollment: " << totalEnr;
return totalEnr;
}
void freeMem(School& Sch) {
delete [] Sch.m_name;
Sch.m_name = NULL;
for (int i = 0; i < *Sch.m_noOfSubjects; i++) {
freeMem (Sub[i]);
}
delete [] Sch.m_subjects;
Sch.m_subjects = NULL;
}
}
Subject.cpp
namespace sdds {
void read(char* str) {
cout << "Enter subject name: ";
read(str, 71, "Name is too long, only 70 characters allowed!\nRedo Entry : ");
}
void read(int& noofSections) {
cout << "Enter number of sections: ";
read(noofSections, 0, 11, "Invalid Number of sections, 1<=ENTRY<=10\nRedo Entry: ");
}
void read(int noOfStdsInSecs[], int noOfSections) {
cout << "Enter the number of students in each one of the " << noOfSections << " sections:" << endl;
for (int i = 0; i < noOfSections; i++) {
cout << i+1 << ": ";
read(noOfStdsInSecs[i], 5, 35, "Invalid Number of students, 5<=ENTRY<=35\nRedo Entry: ");
}
}
void read(Subject& Sub) {
char subjectName[71];
read(subjectName);
Sub.m_subjectName = new char[50];
strcpy(Sub.m_subjectName, subjectName);
Sub.m_noOfSections = new int;
read(*Sub.m_noOfSections);
Sub.m_noOfStdsInSecs = new int [*Sub.m_noOfSections];
read(Sub.m_noOfStdsInSecs, *Sub.m_noOfSections);
}
void freeMem(Subject& Sub) {
delete[] Sub.m_subjectName;
Sub.m_subjectName = NULL;
delete Sub.m_noOfSections;
Sub.m_noOfSections = NULL;
delete [] Sub.m_noOfStdsInSecs;
Sub.m_noOfStdsInSecs = NULL;
}
UPDATED: After turning a Sch.m_noOfSubjects into just an int I keep getting the same heap summary, but now it literally says that everything is fine but because of it I can not submit it, since my output and the output from the submission matrix script do not match. Is there a way to somehow turn it off?
------------------------
End Testing the Program!
==1821==
==1821== HEAP SUMMARY:
==1821== in use at exit: 0 bytes in 0 blocks
==1821== total heap usage: 8 allocs, 8 frees, 166 bytes allocated
==1821==
==1821== All heap blocks were freed -- no leaks are possible
==1821==
==1821== For counts of detected and suppressed errors, rerun with: -v
==1821== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Script done, file is student_output.txt
Checking output:
In line number 33 of your output:
The output should be:
^
But your output is:
[38;5;9m------------------------
^