how to manage a "resource" array efficiently
Posted
by Haiyuan Zhang
on Stack Overflow
See other posts from Stack Overflow
or by Haiyuan Zhang
Published on 2010-01-27T23:14:52Z
Indexed on
2010/04/11
16:03 UTC
Read the original article
Hit count: 376
The senario of my question is that one need to use a fixed size of array to keep track of certain number of "objects" .
The object here can be as simply as a integer or as complex as very fancy data structure. And "keep track" here means to allocate one object when other part of the app need one instance of object and recyle it for future allocation when one instance of the object is returned .Finally ,let me use c++ to put my problme in a more descriptive way .
#define MAX 65535
/* 65535 just indicate that many items should be handled . performance demanding! */
typedef struct {
int item ;
}Item_t;
Item_t items[MAX] ;
class itemManager {
private :
/* up to you.... */
public :
int get() ; /* get one index to a free Item_t in items */
bool put(int index) ; /* recyle one Item_t indicate by one index in items */
}
how will you implement the two public functions of itemManager ? it's up to you to add any private member .
© Stack Overflow or respective owner