Title: windows causing breaking point
Tags: c++;pointers;dynamic
Question: I am writing a program for a class assignment on learning about pointers and memory management. Allocating memory for student and courses is necessary with the new operator in this assignment and the program seems to run properly but after it prints everything the program crashes and it says that that it has triggered a break point. Any help would be appreciated.
The error is located here but I'm not sure what it means
/* verify block type */
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead-&gt;nBlockUse));
#include <iostream&gt;
#include <string&gt;
#include <iomanip&gt;
using namespace std;
struct Student //Structs
string firstName, lastName, aNumber;
double GPA;
};
struct Course
int courseNumber, creditHours;
string courseName;
char grade;
};
Student* readStudent();                                    // Function Prototypes
Course* readCourseArray(int&amp;);
void computeGPA(Student *student, Course *courseArray, int coursesTaken);
void printSummary(Student *studentPTR, Course *courseArray, int courses);
int main()                                                //Main
int courses = 0;
Student *studentPTR = readStudent();
Course *coursePTR = readCourseArray(courses);
computeGPA(studentPTR, coursePTR, courses);
printSummary(studentPTR, coursePTR, courses);
delete studentPTR;
delete coursePTR;
system ("pause");
return 0;
Student* readStudent()                                       // Read Student
Student* student = new Student;
cout<<"\nEnter students first name\n";
cin&gt;&gt;student-&gt;firstName;
cout<<"\nEnter students last name\n";
cin&gt;&gt;student-&gt;lastName;
cout<<"\nEnter students A-Number\n";
cin&gt;&gt;student-&gt;aNumber;
return student;
Course* readCourseArray(int &amp;courses)                           //Read Courses
cout<<"\nHow many courses is the student taking?\n";
cin&gt;&gt;courses;
const int *sizePTR = &amp;courses;
Course *coursePTR = new Course[*sizePTR];
for(int count = 0; count < *sizePTR; count++)  //Enter course information
cout<<"\nEnter student "<<count+1<<"'s course name\n";
cin&gt;&gt;coursePTR[count].courseName;
cout<<"\nEnter student "<<count+1<<"'s course number\n";
cin&gt;&gt;coursePTR[count].courseNumber;
cout<<"\nEnter student "<<count+1<<"'s credit hours\n";
cin&gt;&gt;coursePTR[count].creditHours;
cout<<"\nEnter student "<<count+1<<"'s grade\n";
cin&gt;&gt;coursePTR[count].grade;
}
return coursePTR;
void computeGPA(Student *studentPTR, Course *courseArray, int coursesTaken)             // Compute GPA
double total = 0, hours = 0;
for(int count = 0; count < coursesTaken; count++)
hours += courseArray[count].creditHours;
if (courseArray[count].grade == 'A')
total+=4;
else if (courseArray[count].grade == 'B')
total+=3;
else if (courseArray[count].grade == 'C')
total+=2;
else if (courseArray[count].grade == 'D')
total+=1;
else if (courseArray[count].grade == 'F')
total+=0;
studentPTR-&gt;GPA = (total / hours);
void printSummary(Student *studentPTR, Course *courseArray, int courses)
cout<<"\nReport\n";
cout<<"\nNumber   Name                    Hours  Letter Grade  Point Grade";
cout<<"\n------   ----                    -----  ------------  -----------\n";
for(int count = 0; count < courses; count++)
cout<<setw(3)<<courseArray[count].courseNumber<<"   "<<courseArray[count].courseName<<setw(20)<<"   "<<courseArray[count].creditHours<<setw(5)<<"  "<<courseArray[count].grade<<endl;
cout<<"\nStudent :"<<studentPTR-&gt;firstName<<" "<<studentPTR-&gt;lastName;
cout<<"\nA-Number :"<<studentPTR-&gt;aNumber;
cout<<"\nOverall GPA :"<<studentPTR-&gt;GPA;

Here is the accepted answer: You're allocating courses with operator new[] (which is for arrays), but freeing it with operator delete, instead of operator delete[].
You should deallocate it like that:
delete[] coursePTR;
I also don't understand why you use the sizePTR pointer instead of directly using courses.
BTW, just so you'd know, this is not a 'find my bug' site. You have a code review site for that.

Here is another answer: You need to change
delete coursePTR;
delete [] coursePTR;
new always needs to be used with delete, and new [] with delete [], otherwise bad things happen.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:02/23/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++><pointers><dynamic>', 'Title': 'windows causing breaking point', 'ViewCount': 172, 'id': 9415774, 'user_AboutMe': "<p>Dumb csci student that doesn't know anything at all.</p>\n", 'user_AccountId': 997336.0, 'user_DisplayName': 'sircrisp', 'user_Reputation': 1037.0, 'user_Views': 144.0, 'user_id': 1012968.0}}