C ++:当我创建一个对象时,我的程序崩溃了

时间:2015-04-03 22:00:09

标签: c++ crash

我创建了以下基本的大学系统课程。

当我创建另一个类大学的对象时,我正面临崩溃。

第一个是完美的工作(pmu),由于某种原因,我不知道为什么程序在创建另一个对象时不会崩溃,因为你可以在int main()的末尾看到评价。

谢谢,

class Date
{
public:
int day;
int month;
int year;
Date(){day=0;month=0;year=0;}
Date(int d,int m,int y){day=d;month=m;year=y;}
void Print(){cout<<day<<"-"<<month<<"-"<<year<<endl;}
};
class Student
{
public:
int id;
string name;
double gpa;
Date dob;
Student(){id=0;name="";gpa=0;}
Student(int i,string n,double g, Date d){id=i; name=n; gpa=g; dob=d;}
void Print(){cout<<"id="<<id<<", name="<<name<<", gpa="<<gpa<<", DOB=";dob.Print();}
};
class Department
{
public:
string name;
int numOfStudents;
string chair;
Student students[100];
Department(){name="";numOfStudents=0;chair="";}
Department(string n, string c){name=n; chair=c; numOfStudents=0;}
void Print(){cout<<"Department="<<name<<", # of students="<<numOfStudents<<", Chair="<<chair<<endl;}
void PrintStudents(){for(int i=0; i<numOfStudents; i++) students[i].Print();}
void AddStudent(Student s){students[numOfStudents]=s;numOfStudents++;}
void DeleteStudent(int id){}
};

class College
{
public:
string name;
int numOfStudents;
string dean;
Department departments[10];
int numOfDepartments;
College(){name="";numOfStudents=0;dean="";numOfDepartments=0;}
College(string n, string d){name=n; dean=d; numOfStudents=0;numOfDepartments=0;}
void Print(){cout<<"College="<<name<<", # of students="<<numOfStudents<<", Dean="<<dean<<endl;}
void PrintDepartments(){for(int i=0; i<numOfDepartments; i++) departments[i].Print();}
void AddDepartment(Department s){departments[numOfDepartments]=s;numOfDepartments++;numOfStudents+=s.numOfStudents;}
void DeleteDepartment(string name){}
};
class University
{
public:
string name;
int numOfStudents;
string rector;
College colleges[10];
int numOfColleges;
University(){name="";numOfStudents=0;rector="";numOfColleges=0;}
University(string n, string r){name=n;rector=r;numOfStudents=0;numOfColleges=0;}
void Print(){cout<<"University="<<name<<", # of students="<<numOfStudents<<", Rector="<<rector<<endl;}
void PrintColleges(){for(int i=0; i<numOfColleges; i++) colleges[i].Print();}
void AddCollege(College c){colleges[numOfColleges]=c;numOfColleges++;numOfStudents+=c.numOfStudents;}
void DeleteCollege(string name){}


};

int main()
{
cout<<"here is the object date:\n";
Date today(2,3,2015);
today.Print();

cout<<"here is the object maryam:\n";
Student maryam(123,"maryam", 3.5, today);
maryam.Print();

cout<<"here is the object department:\n";
Department it("IT", "Dr. Loay");
it.Print();

cout<<"here is all the students in the department:\n";
it.AddStudent(maryam);

Student sara(225,"sara", 3.5, today);
it.AddStudent(sara);

Student fatimah(11,"fatimah", 3.5, today);
it.AddStudent(fatimah);


it.PrintStudents();


it.Print();

College cces("CCES", "Dr. Ammar");
cout<<"here is the college information:\n";
cces.Print();



cces.AddDepartment(it);

cces.PrintDepartments();
cout<<"here is the college information:\n";
cces.Print();

University pmu("PMU", "DR. Essa");
pmu.AddCollege(cces);


cout<<"\nhere is the university information:\n";
pmu.Print();
pmu.PrintColleges();

Date today1 (4,2,2015);
today1.Print();

Student khalid (1001,"Khalid",3.6,today);


Student ahmed (1002,"Ahmed",3.6,today);


Student john (1003,"John",3.6,today);


Student tom (1004,"Tom",3.6,today);


Department MEeng("MEEng","Dr. Nader");
MEeng.AddStudent(khalid);
MEeng.AddStudent(ahmed);
MEeng.AddStudent(john);
MEeng.AddStudent(tom);
MEeng.Print();
MEeng.PrintStudents();

College coneng ("CoEng","Dr. Jamal");
coneng.AddDepartment(MEeng);
coneng.Print();
pmu.AddCollege(coneng);
pmu.Print();
pmu.PrintColleges();

Student raja (2001,"Raja",3.7,today1);


Student sultan (2002,"Sultan",3.7,today1);


Student nasser (2003,"Nasser",3.7,today1);


Student jim (2004,"Jim",3.7,today1);

Department Civil ("Civil", "Dr. Yousef");

Civil.AddStudent(raja);
Civil.AddStudent(jim);
Civil.AddStudent(nasser);
Civil.AddStudent(sultan);

College eng("Enginerring","Dr. Khalid");

eng.AddDepartment(Civil);
cout<<"Depratments of kfupm eng "<<endl;
eng.PrintDepartments();
eng.Print();
/*University sa ("KFUPM", "Dr. Waleed"); */// the problem happens when I create another Uni object
//kfupm.AddCollege(eng);


return 0;
}

1 个答案:

答案 0 :(得分:2)

问题是您超出了堆栈大小的限制。我更改了您的main()程序,只需执行此操作:

int main()
{
    cout << sizeof(University) << "\n";
    cout << sizeof(College) << "\n";
    cout << sizeof(Department);
}

使用Visual Studio 2013,这是打印的内容:

567192
56712
5664

University本身的大小超过半兆字节。当您在代码中创建两个或更多时,超出堆栈内存(默认情况下为Visual Studio项目)为1兆字节,我猜您的环境/编译器的大小相似。

所以解决方案是使用new在堆上声明这些对象,或者更好的是,在类中使用std::vector而不是原始数组。

编辑:来自ideone的实例:http://ideone.com/gRUxGR

无论哪种方式,这些对象都非常大,你不能简单地将它们放在堆栈中而放弃。

编辑2: 使用std::vector时,尺寸会急剧减少。这里的代码显示了这个:

http://ideone.com/mS1gYJ

对于Visual Studio,输出为:

80
80
72

此外,使用std::vector无需额外的&#34;计数&#34;您现在在班级中拥有的变量,并且不限制您只有100名学生,10个部门。