作者ric2k1 (Ric)
看板EE_DSnP
標題Re: [問題] pushFront() 和 popFront()
時間Wed May 6 20:45:06 2009
希望你重寫不會很久...
※ 引述《yea107 (ㄚ隆)》之銘言:
: 我們在freeArr(T* p)的地方
: 依照上面的指示...
: //2. Get 'p' which points to the beginning of the memory element
這裡的 beginning of the memory element 不是指 first element of the array...
(英文沒寫好...)
事實上這是以前的 comment, 現在 code 改了這個 comment:
// 2. Get 'p' which points to the beginning of the memory element and
// is the memory address to recycle
已經沒有意義... 可以直接刪掉!!
: and is the memory address to recycle.
: 理論上下一個動作好像就是要把p傳入長度為n的recycleList裡
: 使用的是pushFront(T* p)
: 這麼說起來我們的_first似乎會指到的是array的第一個element
: 也就是(p+SIZE_T)的位置
: 可是它前面還有一個SIZE_T的長度阿???
: 這塊記憶體不需要被算進去嗎???
用 pushFront(p) 然後 _first 就應該是 p 啊...
所以 存到 recycleList 的就是 p
: 這樣我們在使用popFront()的時候吐出去的pointer也是指到第一個element嗎??
: 還是要指到第一個(element-SIZE_T)的地方呢??
: (提示上面是說指到第一個element)
指到第一個(element-SIZE_T)的地方
: 這麼說起來當我們在class memMgr 裡頭的 T* getMem(size_t t)時
: 檢查符合大小的_recycleList[n] (假設有的話)
: 就呼叫_recycleList[n]的popFront()吐出一個T的pointer指向之前存的某個
: array的第一個element
: 而這個pointer最終會回傳給overload過的 new[]
: 只是最後在回傳給new[]的時候這個pointer的值不是又會被加上一個SIZE_T大小嗎
: 所以最後就會指在 p + SIZE_T + SIZE_T 的位置上
: 這樣好像有點怪怪的
: 不知道有沒有人知道我在問什麼...
: 觀念好像不太清楚...
你的觀念基本上是對的...
我想關鍵在於 recycleList 裡頭存的就是 first_element_address - SIZE_T,
所以你 popFront() 出來後也是他, 然後 return 回給 new [] 就會變成
first_element_address 了...
: -------------------------------------------
: 總覺得這整份作業一直在做暴力轉換 Orz
: -------------------------------------------
哈哈, 會嗎?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.224.45.41
1F:推 yea107:所以在pushFront()裡面p所指到的就是first_element_address 05/06 22:57
2F:→ yea107:-SIZE_T的地方了對不對呢??(突然發現就是一直+-SIZE_T才會 05/06 22:58
3F:→ yea107:多了很多暴力轉換XD 05/06 22:58
4F:→ ric2k1:Yes. 05/07 00:50