[問題] vector<char*>, strtok 與 free
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
VC++ 2012
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
NO
問題(Question):
我想我需要釐清觀念,這個問題困擾我三天了
我現在遇到memory leak (ML)的問題。
透過VC內建的memory leak detectiont,我知道程式哪裡有Memory leak
但是我有一個地方搞不清楚
容器用到char* ,例如 vector<char*> 或map<char*,int>
當我用 push_back(char*資料) 或 insert(char*資料,1)
在我下列的程式碼,我一個一個去把Vector裡面的char*刪掉是可以,才不會ML
但是我以為我可以直接free(file_content2)就好,
我的認知是 file_content2 一個連續的大空間
vector<char*>裡面的元素只是指到file_content2的片段
例如"I LOVE YOU"經過strtok之後
"I" "\0" "LOVE" "\0" "YOU"
vector分別指到I,LOVE,YOU,那我應該直接free file_content2就好了吧?
可是這樣做的話會出錯,不能free file_content2
但是不free的話,最後VC跟我說file_content2 ML了
可是我找不到哪裡可以free它 :(
希望各位板友們能指引我一下
以下是片段關鍵程式碼
char *file_content2 = (char*) malloc(...);
strcpy(file_content2,file_content); //file_content不想被strtok破壞
vector<char*> v = explode("\n",file_content2);
//free(file_content2) 加上這行會出錯,就錯在這一行,不是之後的free(v[i])
printf("v %X, %X\n",v[0],relation_list_content2);
//上一行會列印 不一直的位置
for(i=0;i<v.size();++i)
free(v[i]); //這樣OK
//exlode程式碼
//出處 //http://ubuntuforums.org/showthread.php?t=1272422 (我有修改過)
std::vector<char*> explode(char *target, char *str)
{
std::vector<char*> v;
char *pch,*last;
pch = strtok_s(str,target,&last);
while (pch != NULL)
{
v.push_back(strdup(pch));
pch = strtok_s(NULL, target,&last);
}
return v;
}
餵入的資料(Input):
oooo,xxxx,1\n
aaa,bbbb,1\n
預期的正確結果(Expected Output):
沒有Memory leak
錯誤結果(Wrong Output):
free錯誤及memory leak
補充說明(Supplement):
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 202.215.28.49
※ 編輯: cjoe 來自: 202.215.28.49 (12/16 16:43)
※ 編輯: cjoe 來自: 202.215.28.49 (12/16 16:44)
推
12/16 16:45, , 1F
12/16 16:45, 1F
→
12/16 16:45, , 2F
12/16 16:45, 2F
→
12/16 16:46, , 3F
12/16 16:46, 3F
→
12/16 16:46, , 4F
12/16 16:46, 4F
→
12/16 16:46, , 5F
12/16 16:46, 5F
※ 編輯: cjoe 來自: 202.215.28.49 (12/16 16:47)
→
12/16 16:47, , 6F
12/16 16:47, 6F
→
12/16 16:47, , 7F
12/16 16:47, 7F
→
12/16 16:49, , 8F
12/16 16:49, 8F
→
12/16 16:49, , 9F
12/16 16:49, 9F
→
12/16 16:52, , 10F
12/16 16:52, 10F
→
12/16 16:52, , 11F
12/16 16:52, 11F
推
12/16 17:04, , 12F
12/16 17:04, 12F
→
12/16 17:06, , 13F
12/16 17:06, 13F
→
12/16 17:06, , 14F
12/16 17:06, 14F
→
12/16 17:06, , 15F
12/16 17:06, 15F
→
12/16 17:07, , 16F
12/16 17:07, 16F
→
12/16 17:13, , 17F
12/16 17:13, 17F
推
12/16 18:15, , 18F
12/16 18:15, 18F
→
12/16 18:15, , 19F
12/16 18:15, 19F
→
12/16 18:16, , 20F
12/16 18:16, 20F
→
12/16 18:16, , 21F
12/16 18:16, 21F
→
12/16 19:03, , 22F
12/16 19:03, 22F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章