-2

2 Problems:

  1. I ran valgrind on this program an lost around 400 bytes, but I don't know what I did wrong.
  2. For some reason, upon the FIRST (and only the first) initialization of a student object (roster[i] when int i = 0), the loop is only allowing me to pass two parameters into my constructor. I know it sounds strange but the first loop invocation is different from the others.

HEADER

#include<iostream>
#include<string>

using namespace std;

class Student{

public:
Student();
Student(string ln, string fn, string cn) {setLast(ln);setFirst(fn);setCourse(cn);}
void showInfo(){cout << firstname << " " << lastname << " in " << coursename << endl;} 

protected:
void setLast (string l){lastname = l;}
void setFirst (string f){firstname = f;}
void setCourse (string c){coursename = c;}
string firstname;
string lastname;
string coursename;
};

MAIN

int main(){

Student **roster;
int num;
cout << "how many?";
cin >> num;
roster = new Student*[num];
string l,f,c;

for (int i = 0; i < num; i++){
  cout << "\nenter last, first, course: ";
  getline(cin, l);
  getline(cin, f);
  getline(cin, c);
  roster[i] = new Student(l, f, c);
  roster[i]->showInfo();
}
delete [] roster;
};
  • 3
    For `roster[i] = new Student(l, f, c);` there is no corresponding `delete` for each `i`. – DeiDei Dec 05 '16 at 21:38
  • 1
    Please don't use C++ as if it was C with fun new syntax. Use `std::vector> roster;`, along with `roster.emplace_back( std::make_unique(l, f, c) );` You should also test that your calls to `getline` succeeded. – paddy Dec 05 '16 at 21:41
  • @n.m. That's utterly unrelated to the problem. – Nic Dec 05 '16 at 21:50
  • @QPaysTaxes there are two problems described in the post, have you checked both? – n. m. could be an AI Dec 05 '16 at 21:55
  • @n.m. Ah, my bad -- I misunderstood the second question as something other than what it actually is. Still, this question should just be closed as "too broad"; it's two questions in one. – Nic Dec 05 '16 at 21:56
  • Why two levels of indirection here -- `Student **roster`? All you needed was `Student *roster;...roster = new Student[num];...roster[i] = Student(l, f, c);` Then your `delete [] roster;` would have worked. You are overusing `new[]` -- it's bad enough you're using `new[]` at all, but then you go overboard with it. Your goal is to create a dynamic array of Student, right? So that translates to `Student *`, not `Student **`. – PaulMcKenzie Dec 05 '16 at 22:00

1 Answers1

0

The problem is that you are newing an array and you are newing all students in the array. you made a new Student*[] and some new Student, but you only delete[] arr, you forgot to delete x

Before the fix, I want to suggest you to use vector and unique_ptr, which would have prevent all your error in the first place:

std::vector<std::unique_ptr<Student>> roster;

int num;
cout << "how many?";
cin >> num;
roster.reserve(num);

std::string l,f,c;

for (int i = 0; i < num; i++){
  cout << "\nenter last, first, course: ";
  getline(cin, l);
  getline(cin, f);
  getline(cin, c);

  roster.emplace_back(std::make_unique<Student>(l, f, c));
  roster.back()->showInfo();
}

Now, the solution for your code. You should put that at the end of your code:

for (int i = 0; i < num; i++){
    delete roster[i];
}
delete [] roster;
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141