Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy can't I reclaim my dynamically allocated memory using the "delete" keyword?
    primarykey
    data
    text
    <p>I have the following class:</p> <pre><code>class Patient { public: Patient(int x); ~Patient(); private: int* RP; }; Patient::Patient(int x) { RP = new int [x]; } Patient::~Patient() { delete [] RP; } </code></pre> <p>I create an instance of this class on the stack as follows:</p> <pre><code>void f() { Patient p(10); } </code></pre> <p>Now, when <code>f()</code> returns, I get a "double free or corruption" error, which signals to me that something is attempted to be deleted more than once. But I don't understand why that would be so. The space for the array is created on the heap, and just because the function from inside which the space was allocated returns, I wouldn't expect the space to be reclaimed.</p> <p>I thought that if I allocate space on the heap (using the <code>new</code> keyword), then the only way to reclaim that space is to use the delete keyword. Help!</p> <p><strong>As requested, here is the actual code (slightly abridged for brevity's sake)</strong></p> <p>Here's the full class definition (split across a <code>.cpp</code> and <code>.h</code> file, but shown together):</p> <pre><code>class Patient { public: Patient(int numDecisionEpochs); ~Patient(); void recordRP(const int&amp; age, const bool&amp; t); void recordBiopsy(const int&amp; age, const int&amp; result); void recordPSA(const int&amp; age, const double&amp; level); void recordPSA(const int&amp; age); private: int* RP; int* Biopsy; double* PSA; }; Patient::Patient(int numDecisionEpochs) { RP = new int [numDecisionEpochs]; Biopsy = new int [numDecisionEpochs]; PSA = new double [numDecisionEpochs]; } Patient::~Patient() { delete[] RP; } void Patient::recordRP(const int&amp; age, const bool&amp; t) { if(t) RP[age-1] = 1; // RP either yes (1) or no (0) else RP[age-1] = 0; } void Patient::recordBiopsy(const int&amp; age, const int&amp; result) { switch(result) { case 0: case 1: case 2: case 3: case 4: Biopsy[age-1]=result; // only permit results 0,1,2,3,4 break; default: cerr &lt;&lt; "Invalid biopsy result (" &lt;&lt; result &lt;&lt; ") at age " &lt;&lt; age &lt;&lt; "!\n"; } } void Patient::recordPSA(const int&amp; age, const double&amp; level) { PSA[age-1] = level; // record PSA volume } void Patient::recordPSA(const int&amp; age) { PSA[age-1] = -1; // symbol for no screening during epoch } </code></pre> <p>Next, the function where the above class is used. The following function is called directly from <code>main()</code> and passed a <code>Policy</code> object which is completely independent and separate from the <code>Patient</code> class:</p> <pre><code>void simulate1(Policy&amp; P) { // ... Patient patient(260); for(int idx=0; idx&lt;(P.size); idx++) { while(state != 9) // while patient not dead { // ... patient.recordPSA(age,PSA); // ... patient.recordPSA(age); // ... patient.recordBiopsy(age,biopsyResult); // ... patient.recordRP(age,true); // ... patient.recordRP(age,false); // ... } // end patient (while loop) } // end sample (for loop) } // end function </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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