如何使用指向另一个结构的指针释放结构的内存

发布于 2024-10-11 12:42:23 字数 409 浏览 4 评论 0原文

我有两个结构,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巾帼英雄 2024-10-18 12:42:23

首先,您没有释放的内存是 SimpleXy*Link* myList,而不是 simpleXyList 内的内存(您正在释放它引用的内存就好了) )。

一般来说,您需要找到一种方法来释放您正在使用的所有内存。一般来说,您将在引用数据的结构之前释放引用的数据,如下所示:

void FreeSimpleXy(SimpleXyLink *myList) {
    free(myList->simpleXyList);
    free(myList);
}

注意(仅限 C++),但是,如果您使用 new 来分配这些数据,则必须使用删除来释放!

如果您使用 C++,还有更简单的方法。首先,析构函数。您可以像这样更改 SimpleXyLink

struct SimpleXyLink
{
    int num_xy;
    SimpleXY *simpleXyList;
    ~SimpleXyLink() {
        delete simpleXyList;
    }
    SimpleXyLink() {
        simpleXyList = NULL; // run when object is created with new
    }
};

现在您只需 delete someLink; 即可,它将自动释放包含的 simpleXyList。 但是,请记住,您现在不得使用mallocfree - 使用new code> 和 delete 代替:

SimpleXyLink *link = new SimpleXyLink;
link->simpleXyList = new SimpleXYList;
delete link; // all gone!

最后,还有一种近乎神奇的做事方式 - 使用智能指针(也仅限 C++)。这些将被添加到下一版本的 C++ 中,但您现在可以通过使用 增强库

struct SimpleXyLink {
    int num_xy;
    boost::scoped_ptr<SimpleXyList> simpleXyList; // or shared_ptr
};

这些将消除编写析构函数的需要(但是您仍然必须使用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:

void FreeSimpleXy(SimpleXyLink *myList) {
    free(myList->simpleXyList);
    free(myList);
}

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:

struct SimpleXyLink
{
    int num_xy;
    SimpleXY *simpleXyList;
    ~SimpleXyLink() {
        delete simpleXyList;
    }
    SimpleXyLink() {
        simpleXyList = NULL; // run when object is created with new
    }
};

Now you can just do delete someLink; and it will free the contained simpleXyList automatically. However, keep in mind that you must not use malloc and free now - use new and delete instead:

SimpleXyLink *link = new SimpleXyLink;
link->simpleXyList = new SimpleXYList;
delete link; // all gone!

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.

struct SimpleXyLink {
    int num_xy;
    boost::scoped_ptr<SimpleXyList> simpleXyList; // or shared_ptr
};

These will eliminate the need to write a destructor (you still must use new and delete 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.

初雪 2024-10-18 12:42:23

如果是 C++ (我在这里很困惑,因为你使用 free :-))

struct SimpleXY
{
    double x;
    double y;

};

struct SimpleXyLink
{   
    SimpleXyLink() : simpleXyList( new SimpleXY ) { }
    ~SimpleXyLink() { delete simpleXyList; }

    int num_xy;
    SimpleXY *simpleXyList;
};

int main() 
{
    SimpleXyLink* pXYLink = new SimpleXyLink();

    delete pXYLink;
}

If it is C++ (I'm confused here because you use free :-))

struct SimpleXY
{
    double x;
    double y;

};

struct SimpleXyLink
{   
    SimpleXyLink() : simpleXyList( new SimpleXY ) { }
    ~SimpleXyLink() { delete simpleXyList; }

    int num_xy;
    SimpleXY *simpleXyList;
};

int main() 
{
    SimpleXyLink* pXYLink = new SimpleXyLink();

    delete pXYLink;
}
热情消退 2024-10-18 12:42:23

这完全取决于您如何分配内存。释放内存总是必须响应分配。

也就是说,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++. Use new/delete instead of malloc/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 as vector or list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文