Thread: destroying a structue

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    15

    Question destroying a structue

    I'm trying to write a linked list with structures using gcc. I have a node structure:

    Code:
    struct Node
    {
         void *value;
         Node *next;
         Node *prev;
    };
    value is a pointer to some data in memory that is allocated using malloc.
    Each node is created using malloc.

    When I destroy the list i call free on every Node pointer, but the memory that value was pointing to is still there, right?
    If so, is there some sort of destructor I can use with structs?

    ...The goal is to separate the list-specific functions as much as possible and still be able to use the list for different types of data.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    free() releases the memory you malloc'ed back to the OS.

    It's still in the computer, but you can't use it.

    C doesn't have destructors, but you can make a function that calls free() for you and acts like an explicitly called destructor. Like fclose.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    69
    Sorry to hijack the thread, but when you free a Node, do you also have to manually free the pointers within, such as next and prev, or does freeing the Node automatically take care of that for you?

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    nope. you need to explicitly call free() on every thing you malloc.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Oh right I misread your question.

    value is still there... you have to free it. But you just said that.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    haha yeah i sorta realized it was easier than i thought..
    free( (*n).value );

    but that's what i get for coding at 3:30 in the morning.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    even easier:

    free( n->value );

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Beware of linked lists, which is the case with the struct above. You should not free the the next, previous pointers there because they keep the list together.
    So don't randomly go about freeing them, instead just free one node and tie the rest of the list together.
    Of course, if you're meant to destroy the entire list, then it's another matter.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. torrents are destroying the world
    By Trennto in forum A Brief History of Cprogramming.com
    Replies: 141
    Last Post: 07-15-2008, 01:48 PM
  2. Destroying windows created by other processes
    By maxorator in forum Windows Programming
    Replies: 17
    Last Post: 01-03-2007, 02:29 PM
  3. Destroying a vector of classes
    By TheSquid in forum C++ Programming
    Replies: 6
    Last Post: 02-24-2005, 08:26 PM
  4. self destroying program
    By bballzone in forum C++ Programming
    Replies: 28
    Last Post: 08-02-2004, 06:43 PM
  5. Not destroying the window in VC++
    By UnclePunker in forum C++ Programming
    Replies: 4
    Last Post: 09-26-2003, 10:05 AM