Memory management

This is a discussion on Memory management within the C Programming forums, part of the General Programming Boards category; What's the easiest (well not easiest), but best method for memory management if i'm allocating memory for structs, strings, arrays, ...

  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
    21,725
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,046
    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, 11:27 AM
  3. Valgrind says I suck at memory management :)
    By carrotcake1029 in forum C Programming
    Replies: 6
    Last Post: 02-01-2009, 07:10 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21