C ++释放了struct使用的所有内存 [英] C++ free all memory used by struct
问题描述
快速提问;我已经四处搜寻,并且已经找到了一些答案,但是我有点偏执,所以我想确定.
Quick question; I've googled around and found some answers already, but I'm a bit paranoid so I want to be sure.
考虑这种情况:
struct CoordLocation
{
float X;
float Y;
float Z;
};
int main()
{
CoordLocation *coord = new CoordLocation();
delete coord;
return 0;
}
调用delete是否还会清除X,Y,Z字段使用的内存?我发现一些答案提到我只是删除POINTER,而不是这样删除实际引用的对象. 如果...
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会打印5.0,因此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;
}
谢谢!
推荐答案
您只需要delete
用new
分配的内存.
You only need to delete
memory you allocate with new
.
printf会打印5.0,因此a所指向的变量不会被完全破坏.
printf would print 5.0, so the variable pointed to by a is not exactly destroyed.
您实际上正在遇到未定义的行为.尽管该值仍然存在,但是内存已释放并且可以重复使用.
You're actually running into undefined behavior. Although the value is still there, the memory was released and can be reused.
以下内容:
struct CoordLocation
{
float X;
float Y;
float Z;
};
如果省略析构函数,则不会造成内存泄漏.
can't create a memory leak if you omit the destructor.
您的下一个片段:
struct CoordLocation
{
float *X;
float *Y;
float *Z;
};
int main()
{
CoordLocation *coord = new CoordLocation();
delete coord;
return 0;
}
可能会造成内存泄漏,但并非如此.将执行以下操作:
can potentially create a memory leak, but not as it is. The following will:
int main()
{
CoordLocation *coord = new CoordLocation();
coord->X = new float();
delete coord;
return 0;
}
您的第三个示例
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;
}
不会造成内存泄漏,因为您释放了分配的所有内存.如果您要省略析构函数或忘记调用delete coord;
,则它们将发生内存泄漏.
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.
一个好的经验法则:每个new
都调用一个delete
,每个new[]
都调用一个delete[]
,这很安全.
A good rule of thumb: call a delete
for every new
and a delete[]
for every new[]
and you're safe.
这篇关于C ++释放了struct使用的所有内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!