3

Hello i'm trying to create a dynamic array of pointer to an object Student from Gradesclass but i can't figure out how to declare it in the header

that's the header:

class Grades
        {
private:
    Student** array;
    int _numofStud;


public:


    Grades();
    Grades(const Grades& other);
    ~Grades();

and the grades constructor (i'm not sure it's right)

Grades::Grades()
{
    this->array = new Student * [2];
    for (int i = 0; i < 2; ++i)
    {
        this->array[i] = NULL;
    }
    this->array[0]= new Student("auto1", "12345");
    this->array[1]= new Student("auto2", "67890");
    this->_numofStud = 2;
} 

The probleme is that before it even enter to the constructor, it creating me an array of Size 5 in Grades because i have 5 elements in the Student constructor

Student::Student(const char* name, char* id)
{
    this->_numofgrade = 0;
    this->setName(name);
    this->setId(id);
    this->_grades = NULL;
    this->_average = 0;
}

And i can't add or modify this size

I want to put a default size of Grades to an array of 2 pointers to student object that i'll define as default then i'll have an other methods that add new Students by creating them and adding their pointers to the array Th problem is i can't change the size of array and i don't understand why

I hope i was clear in my explanation thanks for your help

Edit: enter image description here

that's the debuger and you can see when it's creating a new object Grades g1 it's creating an array of 5 instead off two fill the 2 first as i asked for and the 3 left i have no idea why they have been created and whats inside them

7
  • 4
    You would be far better of using a std::vector<Student> for your array member! Commented Dec 17, 2019 at 10:00
  • 1
    Related, I can't think of a reason in the posted code why you're using a pointer array at all. A std::vector<Student> would seem more appropriate, and bring the added benefit of being far closer to RO3/5/0 compliant. Commented Dec 17, 2019 at 10:01
  • 1
    "before it even enter to the constructor, it creating me an array of Size 5": I don't understand what you mean with this. What array are you talking about? Also what does "5 elements in the Student constructor" refer to? What are these "elements"? Commented Dec 17, 2019 at 10:01
  • Is your intention here to understand how a dynamically sized data structure, such as std::vector actually works? Do you know the size required in Grades() or do you want like a addStudent(...) function that can be called many times? Commented Dec 17, 2019 at 10:02
  • it's for a school project and we did't learn std::vectoryet so i'm not sure i can use it What i mean by "before enter the constructor" is that when i define Student ** arrayit's creating me automaticlay an array of size 5 and i can't understand how to modify this size The 5 element in student refere to every "this->" Commented Dec 17, 2019 at 10:22

1 Answer 1

2

OK, so to be clear, in any actual programs you should use std::vector or other containers, they have a lot of features I ignored here (being templates, supporting move semantics, not requiring a default constructor, etc.), a lot of saftey (what if a constructor throws an exception? What if I do array.add(array[0])?), while still being pretty well optimised for general purpose usage.

And you should also really look at std::unique_ptr, manual new, delete, is generally asking for leaks and other mistakes, in C++ a manual "free" or "delete" of any resource is almost never needed.

Also note in C++ size_t is often used for sizes/lengths of objects and containers.

So the basic idea of a dynamic array is it changes it's size based on current requirements, so Grades() can just start off empty for example.

Grades::Grades()
    : array(nullptr), _numofStud(0)
{}

Then when adding a new item, a new larger array is made, and all the existing items are copied (roughly what std::vector::push_back(x) does).

void Grades::addStudent(Student *student)
{
    // make a larger array
    Student **newArray = new Student*[_numofStud + 1];
    // copy all the values
    for (int i = 0; i < _numofStud; ++i)
        newArray[i] = array[i]; // copy existing item
    // new item
    newArray[_numofStud] = student;
    ++_numofStud;
    // get rid of old array
    delete[] array;
    // use new array
    array = newArray;
}
Sign up to request clarification or add additional context in comments.

5 Comments

No need to declare the array the the header in private like i did ? class Grades { private: Student** array;
Yes you need the stuff in the header, I used the same two variables (array and _numofStud) you had already.
I made the same addStudent fonction but the problem is that when i make the newArray of size [numofStud +1] it's creating me a new array of size 5 No matter what size i put for the new Student*[_numofStud +1]it's creating an array of size 5 That's what i can't understand
I added a pic of the debug so you can see what i mean
C/C++ has no way for the user to determine "size of allocated array" from new T[n], that is why you need a size variable. Whatever _numofStud +1 is (for my code 1, 2, 3, etc.) is the number of elements that are valid. Looking at anything past that is undefined.

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.