Note that there are some explanatory texts on larger screens.

plurals
  1. PO(C++) Memory on the heap becoming in accessable/being deleted
    text
    copied!<p>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</p> <pre><code>#include "OverNightPackage.h" #include "TwoDayPackage.h" #include &lt;iostream&gt; #include &lt;string&gt; 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 &lt;&lt; " Welcome to the package shipper\n\n"; cout &lt;&lt; " You will now enter your information\n"; cout &lt;&lt; " Please enter your name "; getline(cin, name); cout &lt;&lt; "\n Please enter your address (ex 5228) \n"; getline(cin, address); cout &lt;&lt; "\n Please enter your city \n"; getline(cin, city); cout &lt;&lt; "\n Please enter your state\n"; getline(cin, state); cout &lt;&lt; "\n Please enter your 5 digit zipcode\n"; getline(cin, zip); Destination sender(name, address, city, state, zip); while(sendanother &amp;&amp; numpackages &lt; 10) { sendanother = false; numpackages++; cout &lt;&lt; "\n\n You will now enter the package recievers information\n"; cout &lt;&lt; " Please enter recievers name "; getline(cin, name); cout &lt;&lt; "\n Please enter recievers address (ex 5228) \n"; getline(cin, address); cout &lt;&lt; "\n Please enter recievers city \n"; getline(cin, city); cout &lt;&lt; "\n Please enter recievers state\n"; getline(cin, state); cout &lt;&lt; "\n Please enter recievers 5 digit zipcode\n"; getline(cin, zip); Destination receiver(name, address, city, state, zip); cout &lt;&lt; "\n\n Please enter the weight of your package \n"; cin &gt;&gt; weight; cin.ignore(); char menuchoice; cout &lt;&lt; "\n\n\n How would you like to ship your package?\n"; cout &lt;&lt; " Your options are\n 1.) Regular Shipping -- " &lt;&lt; costperoz &lt;&lt; " cost per ounce\n"; cout &lt;&lt; " 2.) 2 Day Delivery -- " &lt;&lt; flatfee2day &lt;&lt; " flatfee added\n"; cout &lt;&lt; " 3.) OverNight Delivery -- " &lt;&lt; overnightextracost &lt;&lt; " added per ounce\n"; cin &gt;&gt; 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 &lt;&lt; "\n\n Would you like to add another package? Y or N\n\n"; cin &gt;&gt; menuchoice; if(menuchoice == 'Y' || menuchoice == 'y') sendanother = true; else sendanother = false; cin.ignore(); } cout &lt;&lt; " \n\n\n --- Shipment Information Below ---\n\n"; for(int i = 0; i &lt; numpackages; i++) { if(packagelist[i] != NULL) { packagelist[i]-&gt;print(); cout &lt;&lt; "\n Shipment Price -- $" &lt;&lt; packagelist[i]-&gt;calcCost() &lt;&lt; "\n\n"; } } for(int i = 0; i &lt; numpackages; i++) delete packagelist[i]; system("pause"); return 0; } </code></pre> <p>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</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload