Quick question; I've googled around and found some answers already, but I'm a bit paranoid so I want to be sure.
Consider this situation:
struct CoordLocation
{
float X;
float Y;
float Z;
};
int main()
{
CoordLocation *coord = new CoordLocation();
delete coord;
return 0;
}
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...
struct CoordLocation
{
float *X;
float *Y;
float *Z;
};
int main()
{
CoordLocation *coord = new CoordLocation();
delete coord;
return 0;
}
And what if I manually free the memory for each object inside the struct's constructor/destructor?
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;
}
I noticed that for a simple situation such as:
float *a = new float;
*a = 5.0f;
printf("%f", *a);
delete a;
printf("%f", &a);
printf would print 5.0, so the variable pointed to by a is not exactly destroyed.
So my question is: How can I reliably free (as in no memory leaks) ALL the memory used by the struct in this case?
struct CoordLocation
{
float X;
float Y;
float Z;
};
int main()
{
CoordLocation *coord = new CoordLocation();
delete coord;
return 0;
}
Thanks!