动态分配数组作为 C++ 结构成员的分段错误?
Segmentation fault with dynamically allocated arrays as members of structure in c++?
我正在编写一个程序,使用一个结构数组来存储一定数量学生的姓名、身份证号码和一组考试成绩。结构体数组和考试成绩成员数组都需要动态分配。我已经开始使用允许用户为每个学生输入测试分数的功能,但是我在最后一个功能(getScores 功能)中遇到了 cin 问题。使用 Linux 时出现分段错误,因此我假设它与作为结构成员的动态分配的测试数组有关,我只是看不到它。我想知道如何调试它,并解释为什么会发生这种情况,以便将来避免这种情况。
//Preprocessor Directives
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Structure declaration
struct Students
{
string name; //Student name
int idNum; //Student ID number
double *tests; //Pointer to an array of test scores
};
//Function prototypes
Students *getStudents(int &);
double *getTests(int &);
void getInfo(string &, int &, int);
void getScores(double &, string, int);
//Main program section
int main ()
{
int numStudents = 0;
int numTests = 0;
Students *studentFiles = NULL;
//Call the getStudents function
studentFiles = getStudents(numStudents);
//Call the getTests function
studentFiles->tests = getTests(numTests);
for(int i = 0; i < numStudents; i++)
{
//Call the getInfo function
getInfo(studentFiles[i].name, studentFiles[i].idNum, i+1);
}
for(int i = 0; i < numStudents; i++)
{
for(int j = 0; j < numTests; j++)
{
getScores(studentFiles[i].tests[j], studentFiles[i].name, j);
}
}
delete [] studentFiles;
delete [] studentFiels->tests;
return 0;
}
Students *getStudents(int &numStudents)
{
Students *studentFiles = NULL;
//Prompt the user for the number of students
cout<<"Enter the number of students: ";
cin>>numStudents;
//Dynamically allocate an array of structs, one for each student
studentFiles = new Students[numStudents];
return studentFiles;
}
double *getTests(int &numTests)
{
double *tests = NULL;
//Prompt the user for the number of tests
cout<<"Enter the number of tests: ";
cin>>numTests;
cin.ignore();
//Dynamicall allocate an array of integers, one for each test
tests = new double[numTests];
return tests;
}
void getInfo(string &name, int &idNum, int index)
{
//Prompt for each student's name and id number
cout<<"Enter the name of student #"<<index<<": ";
getline(cin, name);
cout<<"Enter the id number of student #"<<index<<": ";
cin>>idNum;
cin.ignore();
}
void getScores(double &test, string name, int numTest)
{
cout<<name<<endl;
cout<<numTest<<endl;
//Prompt for each test score for each student
cout<<"Enter "<<name<<"'s score for test #"<<numTest+1<<": ";
cin>>test;
}
一个错误是您访问了已删除对象的成员studentFiles
。反转行以修复该问题:
delete [] studentFiles->tests;
delete [] studentFiles;
理想情况下,使用std::vector<>
而不是手动动态分配和释放内存。
另请注意,代码仅初始化数组第一个成员的 Student::tests
,其余 Student
个对象未初始化此成员。表达式 studentFiles[i].tests[j]
的结果未定义,很可能导致崩溃。
您需要初始化每个 Student
的 Student::tests
成员。完成后,释放每个 Student
.
的 Student::tests
我正在编写一个程序,使用一个结构数组来存储一定数量学生的姓名、身份证号码和一组考试成绩。结构体数组和考试成绩成员数组都需要动态分配。我已经开始使用允许用户为每个学生输入测试分数的功能,但是我在最后一个功能(getScores 功能)中遇到了 cin 问题。使用 Linux 时出现分段错误,因此我假设它与作为结构成员的动态分配的测试数组有关,我只是看不到它。我想知道如何调试它,并解释为什么会发生这种情况,以便将来避免这种情况。
//Preprocessor Directives
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Structure declaration
struct Students
{
string name; //Student name
int idNum; //Student ID number
double *tests; //Pointer to an array of test scores
};
//Function prototypes
Students *getStudents(int &);
double *getTests(int &);
void getInfo(string &, int &, int);
void getScores(double &, string, int);
//Main program section
int main ()
{
int numStudents = 0;
int numTests = 0;
Students *studentFiles = NULL;
//Call the getStudents function
studentFiles = getStudents(numStudents);
//Call the getTests function
studentFiles->tests = getTests(numTests);
for(int i = 0; i < numStudents; i++)
{
//Call the getInfo function
getInfo(studentFiles[i].name, studentFiles[i].idNum, i+1);
}
for(int i = 0; i < numStudents; i++)
{
for(int j = 0; j < numTests; j++)
{
getScores(studentFiles[i].tests[j], studentFiles[i].name, j);
}
}
delete [] studentFiles;
delete [] studentFiels->tests;
return 0;
}
Students *getStudents(int &numStudents)
{
Students *studentFiles = NULL;
//Prompt the user for the number of students
cout<<"Enter the number of students: ";
cin>>numStudents;
//Dynamically allocate an array of structs, one for each student
studentFiles = new Students[numStudents];
return studentFiles;
}
double *getTests(int &numTests)
{
double *tests = NULL;
//Prompt the user for the number of tests
cout<<"Enter the number of tests: ";
cin>>numTests;
cin.ignore();
//Dynamicall allocate an array of integers, one for each test
tests = new double[numTests];
return tests;
}
void getInfo(string &name, int &idNum, int index)
{
//Prompt for each student's name and id number
cout<<"Enter the name of student #"<<index<<": ";
getline(cin, name);
cout<<"Enter the id number of student #"<<index<<": ";
cin>>idNum;
cin.ignore();
}
void getScores(double &test, string name, int numTest)
{
cout<<name<<endl;
cout<<numTest<<endl;
//Prompt for each test score for each student
cout<<"Enter "<<name<<"'s score for test #"<<numTest+1<<": ";
cin>>test;
}
一个错误是您访问了已删除对象的成员studentFiles
。反转行以修复该问题:
delete [] studentFiles->tests;
delete [] studentFiles;
理想情况下,使用std::vector<>
而不是手动动态分配和释放内存。
另请注意,代码仅初始化数组第一个成员的 Student::tests
,其余 Student
个对象未初始化此成员。表达式 studentFiles[i].tests[j]
的结果未定义,很可能导致崩溃。
您需要初始化每个 Student
的 Student::tests
成员。完成后,释放每个 Student
.
Student::tests