(C++) Memory on the heap becoming in accessable/being deleted
I'm having some issue with memory on the heap. See I'm trying to doing polymorphism, and so I allocate an array of pointers of the base class of the polymorphic hierarchy of classes I'm using. However, I'm running into the issue that when I assign memory on the heap to the pointers in my array of pointers, this memory is being deleted later on in the program. the pointers that hold the reference to the dynamically allocated memory aren't going out of scope, but for some reason the data that I have the pointers point to is becoming inaccessable/deleted
#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;
}
see when I get to the end part where I cycle through the array and try to use the polhymorphic functions, that's where I start getting memory access violation errors (when I exclude the if(paackagelist[i] != NULL). So for some reason the data that I'm assigning on the heap to the array of pointers is going out of scope
Nothing to do with 'scope' (not competely sure what you mean by that in this context).
The problem is where you've placed numpackages++
. You increment it before you add a package to your array, you should increment it afterwards.
Also the bounds checking is incorrect. Your packagelist
array is size 5, but you while loop says numpackages < 10
. Those two values should be the same, and preferably you should declare a constant to make sure that they're the same.
上一篇: 创建一个对象:带或不带`new`
下一篇: (C ++)堆上的内存变为可访问/被删除