开发者

How to Free Memory of A Structure with Pointers to another Structure

I have two s开发者_Go百科tructures

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.


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.


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;
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜