Question

Before you read my code, I want everyone to know that I am not using my code anywhere which is why it doesn't have much comments. It's just an example I wrote up to ask this question and it works the way I want it to. Feel free to try it out.

I have a question about my delete function. Does delete[] pTemp->top3Grades; actually delete/free all the array items? Does it know to stop after 3 elements? How? What if some data is right next to in memory which could be the same data (or same type of data) as the first 3? Is 3 passed secretly into the delete function so it knows when to stop? Or does it just delete pTemp->top3Grades[0], the first element?

And if possible, how would I check if it was deleted? I use XCode which come with a debugger if it helps.

#include <iostream>

using namespace std;

struct Students{
int idNum;
int age;
Students* pNext;
int* top3Grades;
};

void deleteAllStudents(Students* *head){
Students* pTemp = *head;
while (pTemp!=NULL) {
    *head = pTemp->pNext;
    delete[] pTemp->top3Grades; //Does this eliminate all 3 elments
                                //or just the first? How would you
                                //check if it is?

    delete (pTemp);             //Might as well ask...how I can see
                                //this was deleted/freed up?
    pTemp=*head;
}
}

void addStudent(Students* *head, int id, int age, int grade1, int grade2, int grade3){
Students* studentEntry= new Students;
studentEntry->top3Grades= new int[3];   // Yes I could have set this as static when
                                        // defining it in struct up above, but
                                        // this way is related to my question later
studentEntry->top3Grades[0] = grade1;
studentEntry->top3Grades[1] = grade2;
studentEntry->top3Grades[2] = grade3;

studentEntry-> idNum = id;
studentEntry-> age = age;
studentEntry->pNext=NULL;

Students* pTemp;

pTemp = *head;

if (*head==NULL) {
    *head = studentEntry;
} else{
    while (pTemp->pNext!=NULL) {
        pTemp= pTemp->pNext;
    }
    pTemp-> pNext= studentEntry;
}

}

void dispalyAllStudents(Students* *head){
Students* pTemp;

pTemp = *head;

while (pTemp!=NULL) {
    cout<<"ID #: "<<pTemp->idNum<<" Age: "<<pTemp->age<<endl;
    cout<<"Best grades are "
    <<pTemp->top3Grades[0]<<" "
    <<pTemp->top3Grades[1]<<" "
    <<pTemp->top3Grades[2]
    <<endl<<endl;
    pTemp= pTemp->pNext;
}

}


int main()
{
int inputNum, studentID, studentAge, bestGrade1, bestGrade2, bestGrade3;

Students* pHead=NULL;

cout<< "How many records do you want to input? ";
cin >> inputNum;

for (int i = 0; i<inputNum; i++) {
    cout<<endl;
    cout<<"Enter ID Number: ";
    cin>>studentID;
    cout<<"Enter age: ";
    cin>>studentAge;
    cout<<"Enter first best grade: ";
    cin>>bestGrade1;
    cout<<"Enter second best grade: ";
    cin>>bestGrade2;
    cout<<"Enter third best grade: ";
    cin>>bestGrade3;
    addStudent(&pHead, studentID, studentAge, bestGrade1, bestGrade2, bestGrade3);
}

cout<< "--------"<<endl;

dispalyAllStudents(&pHead);

deleteAllStudents(&pHead);
return 0;
}
Was it helpful?

Solution

Yes, delete[] knows how many elements there are if you pass it a pointer that was returned from a call of new[]. In your code, you have essentially:

int * p = new int[3];
// ...
delete [] p;

That's exactly what it is designed to do.

How does it do that? That's not really your business. As for the present example, in the quasi-standard Itanium ABI for C++, there is nothing to do, and you essentially end up with a call of free() on the underlying memory. (So just use your existing understanding of free() to fill in the blanks.) If the type of the array element was not trivially destructible, the compiler would need to store the number of array elements somewhere in some "hidden" place (which the Itanium ABI specifies) so that it can call all the destructors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top