Frage

So here's a C++ exercise on dynamic memory allocation and objects creation. Basically - a custom class Student and a custom class Group which keeps an array of pointers to Students inside. There's obviously a problem in Group's destructor but I've spent hours reading manuals and surfing forums and still cannot understand what I'm doing wrong.

Any comments are welcome.
UPD: the problem is - error on exit. "Debug assertion failed... _BLOCK_TYPE_IS_VALID..."

class Student{
    char *firstName;
    char *lastName;

public:

    Student(): firstName(NULL), lastName(NULL){}

    Student(const char *fname, const char *lname){
        firstName = new char[32];
        lastName = new char[32];
        strcpy_s(firstName, 32, fname);
        strcpy_s(lastName, 32, lname);
    }

    void Show(){
        cout << firstName << " " << lastName << endl;
    }

    ~Student(){

        if(lastName != NULL)
            delete[] lastName;

        if(firstName != NULL)
            delete[] firstName;
    }
};

class Group{
    Student *students;
    int studentCounter;
public:

    Group(){
        students = NULL;
    }

    Group(const int size){
        students = new Student[size];
        studentCounter = 0;
    }

    void Push(Student *student){
        students[studentCounter] = *student;
        studentCounter++;
    }

    void ShowAll(){
        for(int i = 0; i < studentCounter; i++){
            students[i].Show();
        }
    }

    ~Group(){

        if(students != NULL)
            delete[] students;                //problem here?
    }
};

void main(){

    Student jane("Jane", "Doe");
    Student john("John", "Smith");
    Group group(2);

    group.Push(&jane);
    group.Push(&john);

    group.ShowAll();

    _getch();
} 
War es hilfreich?

Lösung

Your Student Class is missing a copy assignment Operator, in absence of which, the default assignment operator provided only does a shallow copy.

When you create the student object and push in onto Group, both the student object and the one Group holds in its array has the same reference to the firstName and lastName array in absence of an assignment operator, which was supposed to create a clone of these data structures.

So when the Student Objects were deleted during stack unwinding, it results in a double delete as the arrays were already deleted when Group was destroyed.

Andere Tipps

The problem is:

students[studentCounter] = *student;

You need to write an assignment operator (operator=) so the internal buffers are copied and not only addresses are copied.

What happens is that the firstName, lastName vars are deleted twice.

class Student{
//...
public:

    Student& operator=(const Student& rhs) {
        delete firstName;
        delete LastName;
        firstName = new char[32];
        lastName = new char[32];
        strcpy_s(firstName, 32, rhs.firstName);
        strcpy_s(lastName, 32, rhs.lastName);
        return *this;        
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top