首页 > 解决方案 > 删除动态数组

问题描述

这是我第一次使用指针,我在删除使用new关键字创建的动态分配数组时遇到了一些问题。对于我正在进行的这个项目,我需要将每个数组都设为动态数组。

我有 3 个动态数组和一个指针,但每当我去删除它们时,第三次删除会导致我的程序崩溃。据我所知,我习惯delete[] ptr;在使用它们后删除每个数组,并且我的指针在删除时都指向数组中的第一个元素。

以下是我在程序中使用第三个指针的所有实例:

void compareAnswers (ifstream& responses, ifstream& answers)
{
/* Creating pointers and filling dynamic arrays with data */

//compare answers to responses
int totalMissed = 0;
int *ptr3 = new int[totalMissed];     //make a new dynamic array for missed questions
for (int i = 0; i <= totalQuestions - 1; i++)
{
    if (*(ptr1 + i) != *(ptr2 + i))       //if an answer is wrong...
    {
        totalMissed++;               // add to the total missed questions
        *ptr3 = i + 1;               // assign the missed question to the current spot in the array
        ptr3++;                      // increment the pointer
    }
}
ptr3 = ptr3 - totalMissed;      //returns ptr3 to position of first element in array

//display contestant's report
displayContestantsReport (contestantID, ptr3, totalMissed, totalQuestions, ptr1, ptr2);

//delete the dynamic arrays
delete[] ptr1;
ptr1 = nullptr;
delete[] ptr2;
ptr2 = nullptr;
delete[] ptr3;
ptr3 = nullptr;
}

这是 displayContestantsReport 函数:

void displayContestantsReport (string ID, int *ptr3, int totalMissed, int totalQuestions, char *ptr1, char *ptr2)
{
    //display ID and score
    double score = (((static_cast<double>(totalQuestions)) - (static_cast<double>(totalMissed)))/static_cast<double>(totalQuestions)) * 100;
    cout << ID << " - " << fixed << setprecision(2) << score << endl;
//display missed questions
if (score != 100)
{
    //display question number
    for (int i = 0; i <= totalMissed - 1; i++)
    {
        if (i == totalMissed - 1)
            cout << *(ptr3 + i) << endl;
        else
            cout << *(ptr3 + i) << " ";
    }

    //display contestant's responses
    int temp = 0;
    for (int i = 0; i <= totalMissed - 1; i++)
    {
        //if question number is a single digit
        temp = *(ptr3 + i);
        if (temp >= 1 && temp <= 9)
        {
            if (i == totalMissed - 1)
                cout << *(ptr2 + (temp - 1)) << endl;
            else
                cout << *(ptr2 + (temp - 1)) << " ";
        }
        else //if question is a two digit letter
        {
            if (i == totalMissed - 1)
                cout << " " << *(ptr2 + (temp - 1)) << endl;
            else
                cout << " " << *(ptr2 + (temp - 1)) << " ";
        }
    }

    //display correct answers
    int temp2 = 0;
    for (int i = 0; i <= totalMissed - 1; i++)
    {
        //if question number is a single digit
        temp2 = *(ptr3 + i);
        if (temp2 >= 1 && temp2 <= 9)
        {
           temp2 -= 1;
            if (i == totalMissed - 1)
                cout << *(ptr1 + temp2) << endl;
            else
                cout << *(ptr1 + temp2) << " ";
        }
        else //if question is a two digit letter
        {
           temp2 -= 1;
            if (i == totalMissed - 1)
                cout << " " << *(ptr1 + temp2) << endl;
            else
                cout << " " << *(ptr1 + temp2) << " ";
        }
    }
}
cout << endl;
}

我对我在代码中搞砸的地方感到困惑,并且可以在需要时提供任何澄清。任何建议都非常感谢!

标签: c++arrayspointersdynamic-memory-allocationdynamic-arrays

解决方案


推荐阅读