(C ++)堆上的内存变为可访问/被删除

我在堆上遇到了一些问题。 看到我试图做多态,所以我分配了我使用的类的多态层次结构的基类的指针数组。 然而,我遇到了这样的问题,即当我将堆中的内存分配给指针数组中的指针时,此内存将在程序中稍后被删除。 指向动态分配内存的指针不会超出范围,但由于某些原因,指向指针的数据正变得无法访问/删除

#include "OverNightPackage.h"
#include "TwoDayPackage.h"
#include <iostream>
#include <string>


using namespace std;

int main()
{
const double overnightextracost = 1.45;
const double flatfee2day = 3.00;
const double costperoz = 1.55;
Package* packagelist[5] = {NULL};
int numpackages = 0;
bool sendanother = true;

string name;
string address;
string city;
string state;
string zip;
double weight;
cout << " Welcome to the package shippernn";
cout << " You will now enter your informationn";
cout << " Please enter your name ";
getline(cin, name);
cout << "n Please enter your address (ex 5228) n";
getline(cin, address);
cout << "n Please enter your city n";
getline(cin, city);
cout << "n Please enter your staten";
getline(cin, state);
cout << "n Please enter your 5 digit zipcoden";
getline(cin, zip);

Destination sender(name, address, city, state, zip);

while(sendanother && numpackages < 10)
{
    sendanother = false;
    numpackages++;
    cout << "nn You will now enter the package recievers informationn";
    cout << " Please enter recievers name ";
    getline(cin, name);
    cout << "n Please enter recievers address (ex 5228) n";
    getline(cin, address);
    cout << "n Please enter recievers city n";
    getline(cin, city);
    cout << "n Please enter recievers staten";
    getline(cin, state);
    cout << "n Please enter recievers 5 digit zipcoden";
    getline(cin, zip);

    Destination receiver(name, address, city, state, zip);

    cout << "nn Please enter the weight of your package n";
    cin >> weight;
    cin.ignore();

    char menuchoice;
    cout << "nnn How would you like to ship your package?n";
    cout << " Your options aren 1.) Regular Shipping -- " << costperoz << " cost per ouncen";
    cout << " 2.) 2 Day Delivery -- " << flatfee2day << " flatfee addedn";
    cout << " 3.) OverNight Delivery -- " << overnightextracost << " added per ouncen";
    cin >> menuchoice;

    if(menuchoice == '1')
    {
        //static Package * package = new Package(sender, receiver, weight, costperoz);
        packagelist[numpackages] = new Package(sender, receiver, weight, costperoz);
    }
    if(menuchoice == '2')
    {
        //TwoDayPackage * package = new TwoDayPackage(sender, receiver, weight, costperoz, flatfee2day);
        packagelist[numpackages] = new TwoDayPackage(sender, receiver, weight, costperoz, flatfee2day);
    }
    if(menuchoice == '3')
    {
        //static OverNightPackage * package = new OverNightPackage(sender, receiver, weight, costperoz, overnightextracost);
        packagelist[numpackages] = new OverNightPackage(sender, receiver, weight, costperoz, overnightextracost);
    }

    cin.ignore();
    cout << "nn Would you like to add another package? Y or Nnn";
    cin >> menuchoice;

    if(menuchoice == 'Y' || menuchoice == 'y')
            sendanother = true;
    else
        sendanother = false;

    cin.ignore();



}
    cout << " nnn --- Shipment Information Below ---nn";

    for(int i = 0; i < numpackages; i++)
    {
        if(packagelist[i] != NULL)
        {
            packagelist[i]->print();
            cout << "n Shipment Price -- $" << packagelist[i]->calcCost() << "nn";
        }

    }




for(int i = 0; i < numpackages; i++)
    delete packagelist[i];

system("pause");
return 0;
}

看看当我到达循环数组的最后部分并尝试使用polhymorphic函数时,这就是我开始获取内存访问冲突错误的地方(当我排除if(paackagelist [i]!= NULL)。将我在堆上分配给指针数组的数据推理出范围


与'范围'无关(不是在这种情况下确定你的意思)。

问题在于你放置numpackages++ 。 在将数组添加到数组之前将其增加,之后应该增加数组。

边界检查也是不正确的。 你的packagelist数组大小为5,但while循环表示numpackages < 10 。 这两个值应该是相同的,最好你应该声明一个常数来确保它们是相同的。

链接地址: http://www.djcxy.com/p/79109.html

上一篇: (C++) Memory on the heap becoming in accessable/being deleted

下一篇: Trying to understand stack and heap in C++