我在堆上有内存问题。看我正在尝试做多态,所以我分配了我正在使用的类的多态层次结构的基类的指针数组。但是,我遇到的问题是,当我将堆上的内存分配给指针数组中的指针时,此内存将在稍后的程序中删除。保存对动态分配的内存的引用的指针不会超出范围,但由于某种原因,我指针指向的数据变得无法访问/删除
#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 shipper\n\n";
cout << " You will now enter your information\n";
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 state\n";
getline(cin, state);
cout << "\n Please enter your 5 digit zipcode\n";
getline(cin, zip);
Destination sender(name, address, city, state, zip);
while(sendanother && numpackages < 10)
{
sendanother = false;
numpackages++;
cout << "\n\n You will now enter the package recievers information\n";
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 state\n";
getline(cin, state);
cout << "\n Please enter recievers 5 digit zipcode\n";
getline(cin, zip);
Destination receiver(name, address, city, state, zip);
cout << "\n\n Please enter the weight of your package \n";
cin >> weight;
cin.ignore();
char menuchoice;
cout << "\n\n\n How would you like to ship your package?\n";
cout << " Your options are\n 1.) Regular Shipping -- " << costperoz << " cost per ounce\n";
cout << " 2.) 2 Day Delivery -- " << flatfee2day << " flatfee added\n";
cout << " 3.) OverNight Delivery -- " << overnightextracost << " added per ounce\n";
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 << "\n\n Would you like to add another package? Y or N\n\n";
cin >> menuchoice;
if(menuchoice == 'Y' || menuchoice == 'y')
sendanother = true;
else
sendanother = false;
cin.ignore();
}
cout << " \n\n\n --- Shipment Information Below ---\n\n";
for(int i = 0; i < numpackages; i++)
{
if(packagelist[i] != NULL)
{
packagelist[i]->print();
cout << "\n Shipment Price -- $" << packagelist[i]->calcCost() << "\n\n";
}
}
for(int i = 0; i < numpackages; i++)
delete packagelist[i];
system("pause");
return 0;
}
当我到达结束部分的时候,我循环遍历数组并尝试使用polhymorphic函数,这就是我开始获取内存访问冲突错误的地方(当我排除if(paackagelist [i]!= NULL)时。因此,由于某种原因,我在堆上分配给指针数组的数据超出了范围
答案 0 :(得分:1)
与'范围'无关(在这种情况下,不能确定你的意思)。
问题出在numpackages++
的位置。在将数据包添加到数组之前递增它,之后应该增加它。
边界检查也是错误的。您的packagelist
数组的大小为5,但您在while循环中显示numpackages < 10
。这两个值应该是相同的,最好你应该声明一个常量,以确保它们是相同的。