作者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