精華區beta EE_DSnP 關於我們 聯絡資訊
※ 引述《eeturtur (The End)》之銘言: : ※ 引述《ric2k1 (Ric)》之銘言: : : 假設你 new 一個大小為 10 的 array, 你至少需要一塊大小為 11 的記憶體 : : 但根據我們 recycle 的原則 所要的空間必為二的指數 : : 所以 memory manager 會回給你大小為 16 的記憶體空間 : : 而 16 對應於第 4 個 _recycleList : : 所以 getRecycleIndex(10) = 4. : 請問一下老師 : 如果我們要new一個大小為2^n的array : 舉例8個好了 : 那我們是不是有要多給他一個記憶體 (那就是9個了??) : 這個時候memory manager是不是要回給16的記憶體空間呢?? : (這樣似乎就不符合了在word裡ack#elem和index的規則了??) If you do "new A[8]", the req#elem = 9, ack#elem = 16, and the index will be 4. I think this is consistent with the Word file, isn't it? : 因為我有點不清楚老師要我們多分配一個記憶點空間的用意 : 所以不知道是不是有誤會了什麼><"" : 另外就是想要請問老師的是 : 既然現在記憶體都是由我們自己來負責管理 : 那麼我們又怎麼知道在new的時候 : compiler所幫我們儲存的array大小應該放在哪 : 他又該如何放?? In the previous case, let sizeof(A) = 8, then 16 * sizeof (A) = 0x20 Bytes Let the acquired memory (0x20 Bytes from memory manager) has the address starting from 0xaa00a00 --- If you return "0xaa00a00", compiler will record the size of the array (i.e. 8) in this address, and the caller will receive a pointer address "4 Bytes" after, i.e. 0xaa00a04. On the other hand, if you return "0xaa00a4", compiler will record the size of the array (i.e. 8) in this address, and the caller will receive a pointer address "4 Bytes" after, i.e. 0xaa00a08. It's up to you which address you want to return, you just need to make sure your "freeArr()" function can retrieve the original beginning address of this memory block (i.e. 0xaa00a00), and recycle it. As for your question: : 那麼我們又怎麼知道在new的時候 : compiler所幫我們儲存的array大小應該放在哪 Please refer to problem #2 in homework #2, and #89 以及 #91 的討論. : 謝謝m(_ _)m You are welcome. Please let me know if you are still not clear... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.121.138.133