為什麼這篇C free char鄉民發文收入到精華區:因為在C free char這個討論話題中,有許多相關的文章在討論,這篇最有參考價值!作者IhateOGC (我討厭)看板C_and_CPP標題[問題] 需要free const *ch...
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
C
GCC
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
NO
問題(Question):
建立了一個struct myStruct Table[]={
{1 , "OGC_0701"},
{2 , "OGC_0702"},
};
1 .程式整個run完之後需要 free Table嗎?
2. 需要對齊嗎? struct size = 5 gcc不支援#pragma (1)該怎對齊?
餵入的資料(Input):
當筆數越來越多的時候...
預期的正確結果(Expected Output):
使用完必須Free乾淨還給使用者記憶體:D
struct myStruct {
int index;
char *string;
};
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 203.77.53.191
※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1406726869.A.ADB.html
※ 編輯: IhateOGC (203.77.53.191), 07/30/2014 21:30:53
※ 編輯: IhateOGC (203.77.53.191), 07/30/2014 21:31:40
※ 編輯: IhateOGC (203.77.53.191), 07/30/2014 21:34:29
但是該如何free *str指向的記憶體呢? 還是說程式結束也會自己free掉?
-這類字串結構 程式用測試工具重複跑幾萬次後會crash掉嗎@@?
※ 編輯: IhateOGC (203.77.53.191), 07/30/2014 21:49:30
還有...請問需要對齊嗎? 我常常用指標
最近看到struct size不是 4的倍數會有對齊的BUG 需要用pragma(size)去對齊
但是linux下gcc說不支援 Q_Q!
※ 編輯: IhateOGC (203.77.53.191), 07/30/2014 21:55:11
以一個struct size 5的元素來說
int *int_ptr = &mystruct.index; (size 4)
char **char_ptr = &mystruct.string;(size 5)
http://blog.xuite.net/jackie.xie/bluelove/7788211
ps.因為我同樣的code也要讓客戶能在Window上跑@@
※ 編輯: IhateOGC (203.77.53.191), 07/30/2014 22:06:29