内存分配问题 Passing/Returning 结构 * 数组

Memory Allocation Issues Passing/Returning a Struct *Array

请帮我做作业。我已经让这个程序在调试模式下工作得很好,但是一旦我使用发布模式,它就会因 abort() 而崩溃。

我知道这可能与内存分配有关,但我对指针的理解还不够。

要求是我必须使用 *array 来动态分配内存。

"Your program should work for any number of students. When the program starts, it should ask the user for the number of students to be processed. Then it should dynamically allocate an array of that size (array of student/score structures)."

然后我需要,"Call a function to input the student name/score pairs and store them in the array."

那么我应该在 main 中还是在函数内部创建数组?我怎样才能 return/pass *数组而不弄乱内存分配?

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

struct Student
{
    string name;    //student's name
    int score;      //student's score
};

//function prototypes
void inputNameScore(Student*, int&);
void sortArray(Student* , int);
double avgScore(Student* , int);
void displayTable(Student* , int, double);

int main()
{   
    Student* arrayOfStudentPtr;     //pointer of type student to receive returned array pointer
    int numberOfStudents;           //number of students to be entered by user
    double average;                 //total score average   

    cout << "Please enter the number of students: ";
    cin >> numberOfStudents;

    arrayOfStudentPtr = new Student[numberOfStudents];  //dynamic array of type Student assigned to pointer

    inputNameScore(arrayOfStudentPtr, numberOfStudents);
    sortArray(arrayOfStudentPtr, numberOfStudents);
    average = avgScore(arrayOfStudentPtr, numberOfStudents);

    displayTable(arrayOfStudentPtr, numberOfStudents, average);    

    return 0;
}

void inputNameScore(Student* arrayOfStudentPtr, int& numberOfStudents)
{   
    for (int i = 0; i < numberOfStudents; i++)
    {
        cout << endl << "Enter the name for student " << i + 1 << ": ";
        cin.ignore();
        getline(cin, arrayOfStudentPtr[i].name);
        cout << endl << "Enter the student's score: ";      
        cin >> arrayOfStudentPtr[i].score;
        while (arrayOfStudentPtr[i].score > 105 || arrayOfStudentPtr[i].score < 0)
        {
            cout << "Student's score can't be negative or greater than 105." << endl;
            cout << endl << "Enter the student's score: ";
            cin >> arrayOfStudentPtr[i].score;
        }
    }
}

void sortArray(Student* arrayOfStudentPtr, int numberOfStudents)
{
    Student Temp;   //holds a student struct object
    bool swap;      //swap is initialized to false at the start of each loop. If it is still false at end of loop we know there is nothing else to sort
    do
    {
        swap = false;
        for (int i = 0; i < numberOfStudents; i++)
        {
            if (arrayOfStudentPtr[i].score > arrayOfStudentPtr[i + 1].score)
            {
                Temp = arrayOfStudentPtr[i];
                arrayOfStudentPtr[i] = arrayOfStudentPtr[i + 1];
                arrayOfStudentPtr[i + 1] = Temp;
                swap = true;
            }
        }
    } while (swap);
}

double avgScore(Student* arrayOfStudentPtr, int numberOfStudents)
{
    int total;      //total of all grades
    double average; //average of all grades
    total = 0;

    for (int i = 0; i < numberOfStudents; i++)
    {
        total = arrayOfStudentPtr[i].score;
    }
    average = total / numberOfStudents;
    //average = static_cast<double>(total) / numberOfStudents;
    return average;
}

void displayTable(Student* arrayOfStudentPtr, int numberOfStudents, double average)
{   
    cout << endl << setw(31) << left << "Name" << setw(6) << right << "Score" << endl;
    cout << "-------------------------------------" << endl;
    for (int i = 0; i < numberOfStudents; i++)
    {
        cout << setw(31) << left << arrayOfStudentPtr[i].name << setw(6) << right << arrayOfStudentPtr[i].score << endl;
    }
    cout << "-------------------------------------" << endl;
    cout << setw(31) << left << "Average: " << setw(6) << right << endl;
}

以下代码将起作用。

void sortArray(Student* arrayOfStudentPtr, int numberOfStudents)
    {
        Student Temp;   //holds a student struct object
        bool swap;      //swap is initialized to false at the start of each loop. If it is still false at end of loop we know there is nothing else to sort
        do
        {
            swap = false;
            for (int i = 0; i < numberOfStudents-1; i++)
            {
                if (arrayOfStudentPtr[i].score > arrayOfStudentPtr[i + 1].score)
                {
                    Temp = arrayOfStudentPtr[i];
                    arrayOfStudentPtr[i] = arrayOfStudentPtr[i + 1];
                    arrayOfStudentPtr[i + 1] = Temp;
                    swap = true;
                }
            }
        } while (swap);
    }