精華區beta EE_DSnP 關於我們 聯絡資訊
I don't quite understand your problem. However, please make sure --- 1. new ==> alloc ==> getMem() ==> "read" _recycleList[] (p = pop_front()) 2. delete ==> free ==> "write" _recycleList[] (push_front(p)) In (1), if (_recycleList[]->_first == 0), pop_front() should return 0, and getMem() should try to acquire memory from _activeBlock. In (2), if (_recycleList[]->_first == 0), push_front(p) should make _recycleList[]->_first = p; ※ 引述《personhuang (Person)》之銘言: : 請問一下 我似乎卡在這邊 : 如果我mtn 一個東西 會去看_recycleList[]內是否有這樣大小的 : 然後會進到_recycleList[m].getList(n) : 若我new 單個obj 這時候 n=0 m=0 : 進去getList後 應該 : 是去判斷裡面有沒有 _arrSize =n的 recyclelist : 用ddd : 在MemRecycleList<T>* ptr=this; ptr->_arrSize一開始就會等於0 : ptr->_first=0; 因為一開始沒東西 : 如果我要free這邊 是會到getMemRecycleList(0)->pushFront(p) : 這邊傳入getMemRecycleList 然後到 : _recycleList[m].getList(n) 進去t的this的_arrSize 會是 0 ==n : 但ptr->_first 也是0 : 所以會return 0 : 然後_recycleList[m]得到0 傳回去再access就會crash掉 So I don't quite understand why you need to "access" the returned pointer from _recycleList[m] in "free"... ==> You are "writing" to _recycleList, not "reading" when calling "free"... : 在講一次 : 1.如果我都判斷他的_arrSize= =n 是就傳回去 不是就繼續找 找不到就傳回NULL : 這樣我在new時 回傳回非NULL(因為一開始this->_arrSize=n) getmem(t) : 會在空的recycleList要值---錯 : 我在free時 傳入的_this 可以因為this->_arrSize=0 然後傳回去使他可以去pushFront : 2.如果我加上判斷 this->_first =0 就傳回NULL : 這樣new他就對了 他不會跟recyclelist要 : 可是在free時 傳入的this->_first也=0 這樣傳回去再access會crash : 不知道邏輯上我錯在哪裡 或是有哪種方法我沒想到 : 希望有人看的懂 ... : 謝謝指教 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.21.240
personhuang:thx 12/14 18:17