我寫一個簡單的函數庫池,這將作爲一個參數被我的其他功能來管理內存的大小。初始化數據結構用C來管理內存
我有保持由用戶初始化的這個大的存儲器池的信息的數據結構。
typedef struct memBlock{
struct memBlock* next;
unsigned int size; // Size of this block
unsigned int is_used; // bool 0 = not used 1 = used
} memBlock;
我也有這個功能,我正在試圖找出如何初始化這個數據結構以及分配足夠的空間來進行初始管理?
int initialize_memory(unsigned long size){
memBlock *ptr; // the beginning of our whole memory to be handled
ptr = malloc(size); // this is the ptr to the original memory first allocated.
ptr->next = NULL;
ptr->size = NULL;
ptr->is_used = 0;
has_initialized = 1; // the memory has been initialized
}
請幫助
對不起,有什麼問題?我們無法知道內存池中需要多少內存。在那兒? :) – 2011-03-07 20:12:51
而問題是...? (「請幫忙」不計。) – dappawit 2011-03-07 20:27:29
對不起這裏的含糊不清的問題,在我的initialize_memory功能我有一個大的內存池時調用該函數將被保留。我想弄清楚初始化調用(ptr-> next ....)在開始時是否正確?基本上我還有另一個函數,它將像'malloc'一樣保留大量的字節,但是會首先檢查這個數據結構,看看我的池中是否有任何內存可用?我的基本問題是,在從我的ptr分配任何內存之前,是否在開始時正確初始化了此數據結構? – Warz 2011-03-07 20:42:07