C++:创建对象时程序崩溃

Posted

技术标签:

【中文标题】C++:创建对象时程序崩溃【英文标题】:C++: My program crashes when I create an object 【发布时间】:2015-06-09 00:29:09 【问题描述】:

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

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

第一个运行良好 (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;

【问题讨论】:

我创建新对象的主要目标是将其内容与前一个对象进行比较。 您现在的主要目标应该是正确缩进您的代码。 您的代码既不可读,又对于一个合理的问题来说太长了。请考虑一下您在写问题时会如何阅读问题。 你可以粘贴崩溃代码吗?我无法通过取消注释代码中的最后 2 行来重现崩溃。 【参考方案1】:

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

int main()

    cout << sizeof(University) << "\n";
    cout << sizeof(College) << "\n";
    cout << sizeof(Department);

使用 Visual Studio 2013,打印出来的内容如下:

567192
56712
5664

University 本身的大小超过半兆字节。当您在代码中创建 2 个或更多时,会超出堆栈内存,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 消除了您现在在课堂上使用的无关“计数”变量的需要,并且不会将您限制为只有 100 个学生、10 个部门。

【讨论】:

以上是关于C++:创建对象时程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

C++ 中的崩溃恢复

当我创建一个整数作为对象的 NSDictionary 时应用程序崩溃

当我创建 DDXMLDocument 的自动释放对象时,我的应用程序崩溃了

在 C++ 中创建 std 线程会使程序崩溃

Qt 应用程序在退出时崩溃

使用 mcmodel=medium 编译时 C++ 程序崩溃 [关闭]