Thread: Memory management

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    47

    Memory management

    What's the easiest (well not easiest), but best method for memory management if i'm allocating memory for structs, strings, arrays, etc.. Is there some way I could just store all the pointers in a linked list and cycle through it free'ing all the memory?

    I actually have it set up in that way, but it is only set up for a single struct, i.e. the linked list only keeps track of the memory allocated for a struct, than free's everything automatically. Just wondering if there was a generic way to do it so i could manage all memory in an application. Is there a garbage collecting library, or something out there already that people use?

    I guess it's kind of like in an OO language like java, using <generic> or "Object" to overshadow anything you might use, than using casting if you want to manipulate something. Just an easy way to group everything together.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tempster09
    Is there a garbage collecting library, or something out there already that people use?
    Yes, there are garbage collectors available. A quick search of the Web should reveal at least one.

    Alternatively, you can switch to C++ and make use of deterministic destruction to implement the "resource acquisition is initialisation" idiom.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I actually have it set up in that way, but it is only set up for a single struct, i.e. the linked list only keeps track of the memory allocated for a struct, than free's everything automatically. Just wondering if there was a generic way to do it so i could manage all memory in an application. Is there a garbage collecting library, or something out there already that people use?
    Sure -- you could just have your linked list node structure contain a void pointer. A void pointer can point to any type, and yet you can still free() it, so it works out. Other information you might want to store might include a reference count (if you're going to all this trouble you might as well), and perhaps the size of the block, along with other information like the file and line where the block was allocated (__FILE__ and __LINE__). Having all this extra information can often help you debug your program, if you feel like implementing it.

    That said, of course there are other reference counters and garbage collectors available on the internet -- but using them isn't as much fun as writing your own. That's what I did in xuni, but I wouldn't recommend looking at that particular part of xuni's code. :P

    A last example:
    Code:
    #define DEBUG_ALLOCATION_CONTEXT
    
    struct memory_node_t {
        void *data;
        size_t size;
        struct memory_node_t *next;
    #ifdef DEBUG_ALLOCATION_CONTEXT
        const char *file;
        int line;
    #endif
    };
    
    /* ... */
    
    #ifdef DEBUG_ALLOCATION_CONTEXT
        void allocate_memory_context(size_t size, const char *file, int line);
        #define allocate_memory(size) \
            allocate_memory_context(size, __FILE__, __LINE__)
    #else
        void allocate_memory(size_t size);
    #endif
    Have fun . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  3. Valgrind says I suck at memory management :)
    By carrotcake1029 in forum C Programming
    Replies: 6
    Last Post: 02-01-2009, 08:10 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM