删除动态数组

问题描述

这是我第一次使用指针,在删除使用 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;
}

我对我在代码中搞砸的地方感到困惑,可以在需要的地方提供任何说明。非常感谢任何建议!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)