I've made a Circular List and I need help implementing a simple memory management. It's the first time I'm doing this so I really have no idea where to start. What I want to do is pre-allocate X amount of Nodes that I can use for my list. Only when the stock of Nodes is depleted, I will allocated new ones. My list has roughly the following structure:
class List
{
class Elem
{
int info;
Elem *next;
static Elem* freeElems;
};
public:
class Node
{
private:
Elem *elem;
};
static void init();
};
What I want to do is create 10 Elem's in the init() function which will be used throughout the List. When I've used all the available Elem's I will allocate new ones. In the mean time when I need to add a Node/Elem to my List I would first take an Elem from the preallocated ones. What I'm thinking is to allocate 10 elements int void init() and use them for my list, but then I also have to overload the new operator so it would use an existing Elem instead of of creating a new one. So far I got this:
void List::init(){
Elem::freeElems = new Elem(0,NULL);
for(int i = 0; i < 10; i ++)
Elem::freeElems->next = new Elem(0,Elem::freeElems->next);
}
Now if someone can tell me how to overload new to use these Elem's instead of allocating, and if this is the right way to do it in the first place.