Stack Trace Error

Hi guys,

I'm coding a program here that has an array of classes representing a student. I have a search function that allows the user to search the array for a name and return the index of that student:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int Search(string target) {
        if (target.length()>0) {
            for (int i=0; i<numStudents; i++) {
                long second=target.find(' ') + 1;
                target[0]=toupper(target[0]);
                target[second]=toupper(target[second]);
                
                if((int)(student[i].getName()).find(target)>=0) {
                    return i;
                }
            } // end for loop
        } // end if statement
        else
            cout <<"No name was entered.\n";
        return -1;// Student was not found on the list
}

I know this function works because I've used it repeatedly and it always returns the correct index.
Now I want the user to be able to delete an entire student element from the array, so I called the search function and shifted all of the students up one from that index. My delete function looks like this:
1
2
3
4
5
6
7
void DeleteStudent(int index) {
	delete (&student[index]);
        for(int i=index; i<numStudents-1; i++) {
            student[i]=student[i+1];
        }
        numStudents--;
}


However, once I call the Delete function, I get an error in my output:
Stack trace:
Frame     Function  Args
0028ADB4  75E91184  (00000110, 0000EA60, 00000000, 0028AED8)
0028ADC8  75E91138  (00000110, 0000EA60, 000000A4, 0028AEBC)
0028AED8  610C92FA  (00000000, 00000110, 0028AEF8, 00000000)
0028AFB8  610C6057  (00000000, 00000000, 00000000, 00000000)
0028B008  610C643C  (00001090, 0028B030, 0028B1BC, 00000000)
0028B0C8  610C6551  (00001090, 00000006, 0028B0F8, 610C65F5)
0028B0D8  610C658C  (00000006, 0028CE80, 0028B168, 0041A828)
0028B0F8  610C65F5  (00000000, 004523A4, 0028B1BC, FFFFFFFF)
0028B168  610F52DD  (0028B310, 00000000, 00000000, 0041A427)
0028B288  00404338  (0028B310, 0028BFF0, 0000000A, 00000000)
0028CD18  0040194C  (61243AAE, 0000002F, 6120E728, 004011B5)
0028CD38  004011C4  (00000001, 6124473C, 00B300F8, 61004BB9)
0028CD68  61007038  (00000000, 0028CDA4, 61006980, 7EFDE000)
End of stack trace


What's going on here?
Are the students dynamically allocated in the first place (i.e. with new). Otherwise, you shouldn't be deleteing them, as they are destroyed automatically when their lifetimes are over.
no they aren't, i originally thought i didn't need the delete either, but without Line 2, the objects aren't shifted at all. My instructor said that I could just use the assignment operator because my object was simple enough to transfer over, but it seems not?
Well, if the Students are not dynamically allocated with new, then you just can't use delete.

Is there any chance I could see the code where you allocate the students, and the code for the student itself?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
class Student {
    string  firstName;
    string  middleName;
    string  lastName;
    string  suffix;
    string  iD;
    float   test[numTests];
    float   quiz;
    float   avg;
    char    letterGrade;
    
public:
    //********************Constructors********************
    Student() {
        firstName="";
        middleName="";
        lastName="";
        suffix="";
        iD="000000000";
        for(int i=0; i<numTests; i++)
            test[i]=0;
        quiz=0;
        avg=0;
        letterGrade=' ';
    }
    //********************Setters********************
    void setfName(string Name) {
        firstName=Name;
    }
    void setmName(string Name) {
        middleName=Name; 
    }
    void setlName(string Name) {
        lastName=Name; 
    }
    void setSuffix(string Name) {
        suffix=Name; 
    }
    void setID(string ID) {
        iD=ID;
    }
    void setTest(float Test, int i) {
        test[i]=Test; 
    }
    void setQuiz(float Quiz) {
        quiz=Quiz; 
    }
    void setAvg() {
        float	lowest=100;
        float	sum=0;
        float	testTotal=0;
        
        for(int i=0; i<numTests; i++) {
            if(test[i]<=lowest) {
                lowest=test[i];
                sum += test[i];
            }
            else
                sum += test[i];
        }
        testTotal=(sum - lowest)/(numTests - 1);
        if(VerifyScores())
            avg=(quiz + 9*testTotal)/10;
        else
            avg=-1;
    }
    void setLetterGrade() {
        if(avg == -1)
            letterGrade='I';
        else if (avg<=100 && avg>=90 )
            letterGrade='A';
        else if (avg<90 && avg>=80 )
            letterGrade='B';
        else if (avg<80 && avg>=70 )
            letterGrade='C';
        else if (avg<70 && avg>=60 )
            letterGrade='D';
        else
            letterGrade='F';	
    }
     //********************Getters********************
    string getName() {
        if(middleName.length()!=0 && suffix.length()!=0)
            return firstName+" "+middleName+" "+lastName+" "+suffix;
        else if(middleName.length()!=0)
            return firstName+" "+middleName+" "+lastName;
        else if(suffix.length()!=0)
            return firstName+" "+lastName+" "+suffix;
        else
            return firstName+" "+lastName;
    }
    string getfName() {
        return firstName;
    }
    string getlName() {
        return lastName;
    }
    string getcfName() {
        if(middleName.length()==0)
            return firstName;
        else
            return firstName+" "+middleName;
    }
    string getclName() {
        if(suffix.length()==0)
            return lastName;
        else
            return lastName+" "+suffix;
    }
    string getID() {
        string temp;
        string newID;
        for(int i=5; i<9; i++)
            temp += iD[i];
        newID="***-**-" + temp;
        return newID;
    }
    float getTest(int i) {
        return test[i];
    }
    float getQuiz() {
        return quiz;
    }
    float getAvg() {
        return avg;
    }
    char getLetterGrade() {
        return letterGrade;
    }
    //********************Modifiers********************
    void bubbleSortTests() {
        float temp;
        int size=numTests;
        for (int i=0; i<size; i++) 
            for(int j=0; j<size-i-1; j++) 
                if (test[j]>test[j+1]) {
                    temp=test[j];
                    test[j]=test[j+1];
                    test[j+1]=temp;
                }
    }
    void printStudent() {
	setAvg(); 
        setLetterGrade(); 
        bubbleSortTests();
        cout << showpoint << left << fixed << setprecision(2) << endl;
        
        cout << setw(10) << "Name: " << getName() << endl;
        cout << setw(10) << "ID: " << getID() << endl;
        cout << setw(10) << "Tests: ";
        for(int i=0; i<numTests-1; i++)
            cout << (int)test[i] << ", ";
        cout << (int)test[numTests] << endl;
        cout << setw(10) << "Quiz: " << (int)quiz << endl;
        cout << setw(10) << "Average: " << avg << endl;
        cout << setw(10) << "Grade: " << letterGrade << endl;
    }
    void writeStudent(ofstream & fout) {
        fout << showpoint << left << fixed << setprecision(2);
        fout << "*** STUDENT PROFILE ***" << endl << endl;
        fout << setw(10) << "Name: " << getName() << endl;
        fout << setw(10) << "ID: " << getID() << endl;
        fout << setw(10) << "Tests: ";
        for(int i=0; i<numTests-1; i++)
            fout << (int)test[i] << ", ";
        fout << (int)test[numTests] << endl;
        fout << setw(10) << "Quiz: " << (int)quiz << endl;
        fout << setw(10) << "Average: " << avg << endl;
        fout << setw(10) << "Grade: " << letterGrade << endl;
    }
    bool DetSuffix(string target) {
        if(target.compare("I")==0)
            return true;
        else if(target.compare("II")==0)
            return true;
        else if(target.compare("III")==0)
            return true;
        else if(target.compare("IV")==0)
            return true;
        else if(target.compare("V")==0)
            return true;
        else if(target.compare("Sr.")==0)
            return true;
        else if(target.compare("Jr.")==0)
            return true;
        else
            return false;
    } 
    bool VerifyScores() {
        int temp=0;
        for(int i=0; i<numTests; i++)
            if(test[i]<0 || test[i]>100)
                temp++;
        if(quiz<0 || quiz>100)
            temp++;
        if(temp!=0)
            return false;
        else
            return true;
    }
};


I declare the object like so: Student student[50]; as an instance of another class.

The reasons for the discrepancies when using either
50
or
numStudents
is that my instructor has not yet taught us dynamic memory allocation. We need to set a physical size for the array and then calculate a logical size for the portion of the array that actually has relevant data in it.
When I tried a student class with exactly the same data members as yours, simple assignment worked fine. Are you sure you're delete function won't work as follows:
1
2
3
4
5
6
7
void DeleteStudent(int index) {
	// delete (&student[index]); // now removed
        for(int i=index; i<numStudents-1; i++) {
            student[i]=student[i+1];
        }
        numStudents--;
}


Notice that I have removed the delete. Note that you may also want to check if numStudents is already zero. If it is, you don't want to decrement it again.
I feel so dumb....I put the delete student function in another global function and forgot to use pass by reference....thanks for your time haha
Okay then, but if I were you I would still ask your teacher about deleteing non dynamically allocated memory. I am still not convinced that is advisable or necessary...
Topic archived. No new replies allowed.