Re: [問題] 大數字加法

看板C_and_CPP作者 (燒賣)時間14年前 (2011/12/25 16:53), 編輯推噓0(003)
留言3則, 1人參與, 最新討論串3/3 (看更多)
※ 引述《freesamael (燒賣)》之銘言: : → steve1012:另外可以請教一下為什麼會delete兩次嗎? 12/25 11:04 : → steve1012:我只有寫一個destructor而已~ 12/25 11:04 default copy contructor 的作法是逐一呼叫 data member 的 copy constructor, 相當於產生一個如下的 copy constructor: HugeInteger(const HugeInteger &o): IntegerArr(o.IntegerArr), IntegerSize(o.IntegerSize) { } pointer 的複製是複製其值,所以兩個物件的 IntegerArr 會指向同一塊記憶體 +---------------------+ +----------------+ | compiler_added_temp | +------>|allocated_memory| |---------------------| | | | | IntegerArr +----------------+ | | | | | | | | | | | | +---------------------+ | | | | | | | | | | | | +---------------------+ | | | | temporary | | | | |---------------------| | +----------------+ | IntegerArr +----------------+ | | | | +---------------------+ temporary.~HugeInteger() 就會把這塊記憶體 delete 掉了, compiler_added_temp.~HugeInteger() 會再 delete 同一塊記憶體。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.250.29.121

12/25 22:31, , 1F
原來如此!!!!!!!!!!!!
12/25 22:31, 1F

12/25 22:31, , 2F
難怪第一篇回文有大大說我delete了不該delete的東西
12/25 22:31, 2F

12/25 22:31, , 3F
原來是這個意思
12/25 22:31, 3F
文章代碼(AID): #1EzkHwaO (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1EzkHwaO (C_and_CPP)