[問題] free會引起crash

看板C_and_CPP (C/C++)作者 (碳管)時間16年前 (2009/03/21 15:49), 編輯推噓8(809)
留言17則, 8人參與, 最新討論串1/1
宣告一個class complex 內有+-*/ typedef real double; class complex { public : real Re; real Im; complex(real a, real b); complex(); complex operator+(complex); complex operator+(real); complex operator-(complex); : : }; complex Eular(real ); complex abs(complex ); 現在開始用這class做些計算 complex *y; y = (complex*)calloc(n,sizeof(complex)); : : : TF[i] = abs(y[i]) .... free(y); 編譯可過,但會掛在math.h裡面的 abs中 查找了半天,發現把free那行comment out掉就沒事 那請問為何會這樣哩QQ 是這樣我就無法free記憶體了嗎QQ thx -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.117.67.58 ※ 編輯: Carbontube 來自: 59.117.67.58 (03/21 15:52)

03/21 16:06, , 1F
用的compiler是 ?
03/21 16:06, 1F

03/21 16:38, , 2F
請用new
03/21 16:38, 2F

03/21 17:52, , 3F
C++請用new/delete
03/21 17:52, 3F

03/21 18:41, , 4F
n 是否 >= i ??? 超出你配置的範圍
03/21 18:41, 4F

03/21 20:53, , 5F
依照經驗來看 超出你配置的範圍+1
03/21 20:53, 5F

03/22 03:59, , 6F
看不懂樓上大大,超過範圍該是直接在loop內crash
03/22 03:59, 6F

03/22 03:59, , 7F
且與free與否無關吧...?
03/22 03:59, 7F

03/22 04:00, , 8F
怎會不free就沒事,free就crash
03/22 04:00, 8F

03/22 09:08, , 9F
不一定
03/22 09:08, 9F

03/24 01:54, , 10F
這個就要請高手解譯為什麼了!我只知道 超過配置時會出事
03/24 01:54, 10F

03/24 01:55, , 11F
但不一定在哪裡出事!有時候可能不在 function內出事
03/24 01:55, 11F

03/24 01:56, , 12F
當在function 以外出事,這個bug就難找了
03/24 01:56, 12F

03/24 01:58, , 13F
你可能要用肉眼看 memery window的變化是不是超過當初所
03/24 01:58, 13F

03/24 01:59, , 14F
new 的範圍, 或是new 和 delete 之間的code 一行一行mark
03/24 01:59, 14F

03/24 02:00, , 15F
看看是mark 哪一行後,就沒事了,那裡肯定有問題
03/24 02:00, 15F

03/24 23:15, , 16F
我想你還是乖乖用new/delete比較好
03/24 23:15, 16F

03/26 00:42, , 17F
沒錯,是中間迴圈超過範圍了,謝謝高手們:)
03/26 00:42, 17F
文章代碼(AID): #19n9ltqb (C_and_CPP)
文章代碼(AID): #19n9ltqb (C_and_CPP)