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

前端 未结 3 997
[愿得一人]
[愿得一人] 2021-02-11 09:54

I have two structures

struct SimpleXY
{
    double x;
    double y;

};

    struct SimpleXyLink
    {
            int num_xy;
            SimpleXY *simpleXyList         


        
相关标签:
3条回答
  • 2021-02-11 10:10

    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.

    0 讨论(0)
  • 2021-02-11 10:10

    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 讨论(0)
  • 2021-02-11 10:12

    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;
    }
    
    0 讨论(0)
提交回复
热议问题