C_and_CPP 板


LINE

#include<stdlib.h> #include<stdio.h> /* 要注意mem_alloc及mem_free的對應,要用方法發現或防止忘了mem free。 */ void mem_free(int *); int main(void) { int *ptr; ptr=(int *)malloc(sizeof(int)); *ptr=1000; printf("%d\n",*ptr); mem_free(ptr); *ptr=5; printf("%d\n",*ptr); system("pause"); return 0; } void mem_free(int *ptr1) { if(ptr1!=NULL) { free(ptr1); ptr1=NULL; } //*ptr1=5; } 此程式出來的答案是 1000 5 但是有疑問的就是在主程式的mem_free(ptr);下面 明明已經把mem_free了 為何可以使用*ptr=5 設值 但若是在mem_free(int *ptr1) function中的最後 若是設成*ptr1=5 卻會interrupt ? 這是什麼原因呢??? --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 124.9.4.125
1F:推 lausai:mem_free裡把ptr1設為NULL 並不影響ptr 去dereference一個 12/30 19:33
2F:→ lausai:一個NULL pointer會出錯 但是ptr並不是NULL pointer(即使 12/30 19:33
3F:→ lausai:在mem_free執行完之後) 12/30 19:34
4F:推 VictorTom:承接樓上, 在mem_free後的*ptr1=5會馬上出錯, 是因為你 12/30 22:34
5F:→ VictorTom:企圖對NULL寫值; main裡在mem_free不會出錯, 1是因為在 12/30 22:35
6F:→ VictorTom:mem_free()裡給的NULL並不影響到main裡的ptr, 只是mem 12/30 22:35
7F:→ VictorTom:manager已經記錄了該記憶體已被還掉, 只要該位址尚未做 12/30 22:36
8F:→ VictorTom:其他用途, *ptr=5; 暫時有可能不會出錯, 但是對一塊不屬 12/30 22:37
9F:→ VictorTom:於你可以合法使用的記憶體做讀寫, 都是不應該的:) 12/30 22:37
10F:推 althon:那free(ptr1)時,為什麼不會把ptr所指向的空間釋放??? 12/30 23:14
11F:→ althon:我不太懂free(ptr1)釋放那一塊空間? 12/30 23:14
12F:→ althon:void mem_free(int *ptr1)會建立*ptr1指標,指向ptr所指空 12/30 23:15
13F:→ althon:間不是嗎? >"< 我越來越迷網了XD 12/30 23:15
14F:→ akasan:你可以在main裡加mem_free前後加printf("%d\n",ptr); 12/30 23:27
15F:→ akasan:就會知道大概發生什麼事了 12/30 23:27
16F:推 althon:不是%p嗎? 我看過都一樣=.= 12/30 23:39
17F:推 VictorTom:malloc回來的空間有沒有被釋放, 跟指標是否重設成NULL是 12/31 00:18
18F:→ VictorTom:兩件不同的事, malloc回來賦值的指標直接重設成NULL, 這 12/31 00:19
19F:→ VictorTom:個空間也不會被free; 你free(ptr)了malloc來的空間, ptr 12/31 00:19
20F:→ VictorTom:也不會自動變成NULL; 唯一相關的是, 你如果沒把ptr存好 12/31 00:20
21F:→ VictorTom:就在free前又重給值, 就會有memory leak的問題發生. 12/31 00:20
22F:→ VictorTom:至於印出ptr值都一樣, 其實是pass by value或者pass by 12/31 00:21
23F:→ VictorTom:reference的問題; 請試想寫一個func由caller傳入callee 12/31 00:22
24F:→ VictorTom:兩數使其能夠在caller看到交換的結果, 大概就是這樣:) 12/31 00:22
25F:→ johnhmj:請記得在主程式 int *ptr = NULL 要加初值~ 12/31 01:22







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:BuyTogether 或 站內搜尋

TOP