Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ free all memory used by struct
    primarykey
    data
    text
    <p>Quick question; I've googled around and found some answers already, but I'm a bit paranoid so I want to be sure.</p> <p>Consider this situation:</p> <pre><code>struct CoordLocation { float X; float Y; float Z; }; int main() { CoordLocation *coord = new CoordLocation(); delete coord; return 0; } </code></pre> <p>Will calling delete also clear the memory used by the fields X, Y, Z? Some answers I found mentioned that I'd just delete the POINTER, not the actually referenced object this way. What if...</p> <pre><code>struct CoordLocation { float *X; float *Y; float *Z; }; int main() { CoordLocation *coord = new CoordLocation(); delete coord; return 0; } </code></pre> <p>And what if I manually free the memory for each object inside the struct's constructor/destructor?</p> <pre><code>struct CoordLocation { CoordLocation() { *X = new float; *Y = new float; *Z = new float; } ~CoordLocation() { delete X; delete Y; delete Z; } float *X; float *Y; float *Z; }; int main() { CoordLocation *coord = new CoordLocation(); delete coord; return 0; } </code></pre> <p>I noticed that for a simple situation such as:</p> <pre><code> float *a = new float; *a = 5.0f; printf("%f", *a); delete a; printf("%f", &amp;a); </code></pre> <p>printf would print 5.0, so the variable pointed to by a is not exactly destroyed.</p> <p>So my question is: How can I reliably free (as in no memory leaks) ALL the memory used by the struct in this case?</p> <pre><code>struct CoordLocation { float X; float Y; float Z; }; int main() { CoordLocation *coord = new CoordLocation(); delete coord; return 0; } </code></pre> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    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