开发者

how to manage a "resource" array efficiently

开发者 https://www.devze.com 2022-12-18 03:10 出处:网络
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 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 .


Try some memory allocation model, there are guite a few of them. The simplest way would be to store objects into items as they go by and store before them the size of that object imho.

0

精彩评论

暂无评论...
验证码 换一张
取 消