0

I need to add multiple students to the course class but it keeps overwriting the last entry.

I tried to set the address to the object and also using the bracket operator but this causes a memory leak. The problem is with the AddStudent function

class Student {
public:
  Student() { name = "unknown"; };
  Student(string n) { name = n; };
  void Print() { cout << name << endl; };
  string GetName() { return name; };
private:
  string name;
};

class Course {

public:
  Course(int i) {
    id=i;
    nstudents=0;
    capacity=0;
  };

  void AddStudent(Student s) {
    students=&s;
    nstudents++;
  };

private:
  int capacity;
  int nstudents;
  Student* students;
  int id;
};

It only lets me add one student.

1
  • It's worse than you think. The one student record you added officially ceases to exist as soon as AddStudent returns. You're saving the address of an automatic (temporary to scope) variable that is no-more after function return. Dereferencing that pointer invokes undefined behavior. Commented May 1, 2019 at 4:19

3 Answers 3

1

"students=&s;" you are taking the address of a copy-by-value variable which becomes dangling after you leave the function. You don't store the students. You have to put a container in it something like std::vector and copy/move your students in it.

So here with a std::vector with copy and move.

class Course {

    public:
    Course(int i) {
        id=i;
    };

    void AddStudent(const Student& s) {
        students.push_back(s);
    };

    void AddStudent(Student&& s) {
        students.push_back(std::move(s));
    };

    private:
    std::vector<Student> students;
    int id;
};
Sign up to request clarification or add additional context in comments.

Comments

0

You need to allocate memory for your Students array.In your constructor class add : students = new Student[SomeInitialSize] .Also in your add method you need to check if the size of your students is enough to store another student s,and if there is not you need to allocate more memory.

10 Comments

or, even better, use vector<Student> students to declare your students and then use students.push_back(s) in your AddStudent() method.
"students = new Student[SomeInitialSize]" please do not use naked arrays and write the resize code on your own. Here a std::vector is the perfect match.
Yeah I know what you mean,but I went with a Student array since he probably needs to write a student array because of an assignment
I added students = new Student[5] to the course constructer but it says students[nstudents]=&s; isnt assignable
@ocixem the problems you're experiencing have less to do with writing code, and more to do with understanding the semantics and syntax of the language itself. C++ is the worst programming language to cook spaghetti with (eg. type something, throw it at the wall and see if it sticks). This could easily balloon into a full-on tutorial on the most basic of C++ fundamentals, and that isn't what this site is about. I think you need to spend some more time in your reading materials.
|
0

When you add your first student, students is set to point to s inside the AddStudent function. The problem is since s is local to the function, it is destroyed when the function returns. So students is now a dangling pointer pointing to nowhere since the memory it points to is no longer used to store the Student object. In order to keep the memory after the AddStudent function returns, you would have to dynamically allocate it with new, but there are other problems with that.

Let's say you dynamically allocate the memory for the new student. When we add a second student, some more memory will be allocated to store that. That new memory could end up being in a totally different place. Your AddStudent function will set the students pointer to point to the new Student object, but now we've forgotten where the existing student is stored.

So how do we fix this? We could allocate an array of Student objects, leaving extra space for new students. This way, all the students are stored in a contiguous piece of memory, and students will always point to the first object. In our AddStudent function, we would put the new student after the last student we currently have by doing something like students[nstudents] = s, before incrementing nstudents.

The problem with this is that if we exceed the capacity of the array, we would have to allocate a new larger array and copy everything over since we can't expand an existing block of allocated memory. Or you could just make the array fixed sized. But there's a better solution: std::vector.

std::vector is a standard library container that manages the memory for you. You can store your students there and easily add one using push_back. You can learn how to use vectors by asking your instructor, reading a good book, or finding a tutorial online (do note that there are many bad ones out there).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.