1

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.

3
  • You tagged this c++ but are using free (and presumably malloc) did you mean to tag this c instead? Commented Jan 13, 2011 at 14:02
  • @KitsuneTYMG, I think either one is OK. But anyway, I've changed the tag to c Commented Jan 13, 2011 at 14:03
  • Well as you can see from the answers, if you tag it c++ and use free people are going to be confused about want you want. While free works fine in c++ compilers, the way to allocate in c++ is new Commented Jan 13, 2011 at 14:06

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

smart pointers-- interesting! But is it just garbage collector in action, and hence will it inherit all the garbage collector's problem?
@Graviton C++ does not have a garbage collector.
Smart pointers just automate the destructor technique I showed you - when a struct is destructed, the destructors for all (non-pointer) members are called as well. The scoped_ptr destructor then deletes the pointer hidden inside the scoped_ptr. Incidentally, there are in fact garbage collectors for C++ (eg hpl.hp.com/personal/Hans_Boehm/gc) - but they tend to not perform as well as in other languages that are designed to give more useful information to the GC.
Why is it recommended to do simpleXyList = NULL; // run when object is created with new?
2

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.