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!
You only need to
delete
memory you allocate withnew
.You're actually running into undefined behavior. Although the value is still there, the memory was released and can be reused.
So the following:
can't create a memory leak if you omit the destructor.
Your next snippet:
can potentially create a memory leak, but not as it is. The following will:
Your third example
won't create a memory leak because you free all the memory that you allocate. If you were to omit the destructor or forget to call
delete coord;
, they you'd have a memory leak.A good rule of thumb: call a
delete
for everynew
and adelete[]
for everynew[]
and you're safe.The pointer itself is allocated with automatic storage duration, there is nothing to free there. The struct is those three fields, which are freed when you call
delete
. You only calldelete
on something that was returned bynew
.When you allocate something you allocate enough memory to hold it, which means enough memory to hold all of its fields (and some housekeeping memory that is implementation specific, but you don't worry about that). When you delete it you free the same amount of memory that you allocated.
One thing to note however is a case like this (in C++ you wouldn't create a type like this, but the example is relevant):
In this case you have a leak. You delete
f
, which consists of enough memory to hold a singlechar*
, but what thatchar*
points to has been dynamically allocated as well. So, you need to free it too:Again, in C++ you would probably not design a type this way, instead favoring types like
std::string
and principles such as RAII, but the example is relevant.In this example:
The memory is freed. There are no leaks in that code. On the example you gave, where your struct contained pointers, as long as you free them in the destructor(as you did in your example), there will be no leaks.
In this snippet:
The value of
a
stays the same after the delete, sincedelete
(ing) a pointer will only mark a memory address as "freed", it will not modify its contents. Accessing freed pointers is undefined behaviour.