[問題] 一個有點奇怪的記憶體釋放問題
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
GCC
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
non
問題(Question):
記憶體釋放
餵入的資料(Input):
non
預期的正確結果(Expected Output):
成功的印出Im a boy
並成功記憶體釋放
錯誤結果(Wrong Output):
程式碼(Code):(請善用置底文網頁, 記得排版)
想請問一個記憶體釋放的問題
下面是一個C的split實做
接著我想在主程式裡面使用這個函式
並且在最後做記憶體釋放動作
int split(char **arr,char *str, const char *del)
{
int len=0;
char *s = strtok(str, del);
*arr = (char *)malloc(sizeof(sizeof(void *)));
while (s != NULL)
{
*arr = s;
arr++;
len++;
s = strtok(NULL, del);
}
arr -= len;
return len;
}
int main()
{
char **ptr = (char **)malloc(sizeof(void *));
char line_LTC[1000]="Im;a;boy";
length = split(ptr,line_LTC, ";");//以;做分割字串
for (j = 0; j < length; j++)//印出Im a boy
{
//printf(*(ptr+j));
printf((ptr[j]));
printf("\n");
}
for (j = 0; j < length; j++)
{
free((ptr[j]));
}
free(ptr);
}
照理來說
我感覺起來ptr會宣告成二維記憶體陣列
所以利用二維記憶體free的方式做釋放
但是free一直產生問題,很想知道我該怎麼做,才能成功釋放
補充說明(Supplement):
--
我將再起
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.34.119.101
推
01/16 23:22, , 1F
01/16 23:22, 1F
→
01/16 23:24, , 2F
01/16 23:24, 2F
→
01/16 23:31, , 3F
01/16 23:31, 3F
→
01/16 23:33, , 4F
01/16 23:33, 4F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章