如何使用指向另一个结构的指针释放结构的内存
我有两个结构,
struct SimpleXY
{
double x;
double y;
};
struct SimpleXyLink
{
int num_xy;
SimpleXY *simpleXyList;
};
我想知道释放 SimplyXyLink
占用的内存的正确方法是什么?我目前正在使用
void Free(SimpleXyLink *myList)
{
free(myList->simpleXyList);
}
但我认为这是错误的,因为它不会释放 simpleXyList
元素内的内存。
I have two structures
struct SimpleXY
{
double x;
double y;
};
struct SimpleXyLink
{
int num_xy;
SimpleXY *simpleXyList;
};
I wonder what is the proper way to free the memory hold by SimplyXyLink
? I am currently using
void Free(SimpleXyLink *myList)
{
free(myList->simpleXyList);
}
But I think this is wrong because it doesn't free the memory inside the element of simpleXyList
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您没有释放的内存是 SimpleXy*Link*
myList
,而不是 simpleXyList 内的内存(您正在释放它引用的内存就好了) )。一般来说,您需要找到一种方法来释放您正在使用的所有内存。一般来说,您将在引用数据的结构之前释放引用的数据,如下所示:
注意(仅限 C++),但是,如果您使用
new
来分配这些数据,则必须使用删除来释放!如果您使用 C++,还有更简单的方法。首先,析构函数。您可以像这样更改
SimpleXyLink
:现在您只需
delete someLink;
即可,它将自动释放包含的 simpleXyList。 但是,请记住,您现在不得使用malloc
和free
- 使用new
code> 和delete
代替:最后,还有一种近乎神奇的做事方式 - 使用智能指针(也仅限 C++)。这些将被添加到下一版本的 C++ 中,但您现在可以通过使用 增强库。
这些将消除编写析构函数的需要(但是您仍然必须使用new和delete!),但它们也带有其他限制。使用前请仔细阅读我链接的文档,如果您仍然不确定,请随时提出另一个问题。
First, the memory you're not freeing is the SimpleXy*Link*
myList
, not the memory inside the simpleXyList (you're freeing the memory referred to by that just fine).In general, it's up to you to figure out a way to free all the memory you're using. In general, you'll free the referred-to data before the structure that refers to it, as in:
Note (C++ only), however, that if you used
new
to allocate these, you must use delete to free instead!If you're using C++, there's also more foolproof ways. First, destructors. You could change
SimpleXyLink
like so:Now you can just do
delete someLink;
and it will free the contained simpleXyList automatically. However, keep in mind that you must not usemalloc
andfree
now - usenew
anddelete
instead:Finally, there's one more almost-magical way of doing things - using smart pointers (also C++ only). These will be added to the next version of C++, but you can use them today by using the boost library.
These will eliminate the need to write a destructor (you still must use
new
anddelete
however!), but they carry with them other restrictions as well. Read the documentation I linked carefully before using, and feel free to open another question if you're still not sure.如果是 C++ (我在这里很困惑,因为你使用 free :-))
If it is C++ (I'm confused here because you use free :-))
这完全取决于您如何分配内存。释放内存总是必须响应分配。
也就是说,
free
在 C++ 中几乎肯定是错误的。使用new
/delete
而不是malloc
/free
。此外,似乎您正在为多个元素分配内存(至少名称
…List
暗示了这一点),因此您可能最好使用 C++ 容器结构,例如矢量
或列表
。This entirely depends on how you allocated the memory. Freeing memory always has to echo the allocation.
That said,
free
is almost certainly wrong in C++. Usenew
/delete
instead ofmalloc
/free
.Furthermore, it seems as though you’re allocating memory for several elements her (at least the name
…List
implies this) so you will probably be better off using a C++ container structure, such asvector
orlist
.