Alice1nw0
Alice1nw0

Reputation: 69

Dynamic memory allocation to array of pointers to object

I have a class named Student

class Student
{   string name;
    unsigned long int ID ;
    string email;
    unsigned short int year;
    public : 
         Student() // Constructor
         string getName(void);
         unsigned long int getID(void);
         string getEmail(void);
         unsigned short int getYear(void);   
{

and another class named eClass

class eClass {
 private:
 string eclass_name;
  Student* students[100];
  unsigned int student_count;

   public:
    eClass(string name)
    {
        student_count  =0 ; 
        eclass_name = name  ; 
    }

        bool exists(Student obj)
    {
        unsigned long int code = obj.getID();
        bool flag = TRUE ;
        for (unsigned int i = 0 ; i<=student_count ; i++ )
        {
            unsigned long int st = (*students[i]).getID();
            if (code==st)
            {
                flag = FALSE;
            }
        }
        return flag;
    }


        void add(Student& obj)
    { 
        bool res = exists(obj);
        if (res)
        {
            students[student_count] = new Student(); //probably the problem is here
            *students[student_count] = obj ;  
            student_count++ ; 
        }
    }

    string getEclassName(void) { return eclass_name; }
    unsigned int getStudentCount(void) { return student_count; }
    Student getStudent(int i) { return *students[i-1]; }


     };    

The statement Student* students[100]; must look exactly like this . For example I can't write something like this: Student* students[100]={} ;

And main() looks like this

    int main()
   {
     Student    JohnDoe("John Doe", 12345,  2,  "[email protected]");
     eClass Cpp("C++"); 
     Cpp.add(JohnDoe);


     }

Basically I have an array of pointers to Student objects and I want to allocate dynamically memory every time I want to add a new Student object.

When I compile I get no errors but when I try to run the program the only thing I get is "Program_name.exe" stopped running...

I'm pretty sure the problem has to do with memory allocation but I'm not able to find it and solve it.

Any suggestions ?

Upvotes: 1

Views: 145

Answers (2)

soulsabr
soulsabr

Reputation: 904

First, you should initialize all of your student pointers to either NULL or nullprt. This is not strictly needed but is a very good habit to get into. You'll thank yourself later.

Second, why are you returning false if the student exists? Kind of confusing I'd imagine. Also, you can use the break statement after you find your student exists; no need to check the rest of them.

Also, on your add, you may want to check to ensure you don't have MORE than 100 students. This will overwrite memory and bad things will happen.

Upvotes: 0

JSF
JSF

Reputation: 5321

The main bug in exists was the loop went one too far, using an uninitialized pointer. But also it is very bad style for exists to take its input by value. Fixing both of those:

bool exists(Student const& obj)
{
    unsigned long int code = obj.getID();
    bool flag = TRUE ;
    for (unsigned int i = 0 ; i<student_count ; i++ )
    {
        unsigned long int st = (*students[i]).getID();
        if (code==st)
        {
            flag = FALSE;
        }
    }
    return flag;
}

You should declare getID() const inside student in order to be able to code exists correctly.

unsigned long int getID() const;

Upvotes: 2

Related Questions