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 . . . .