2015-05-16 29 views
0

我的問題是我必須釋放一個雙指針。我已經試過了:不能釋放雙指針

char** files = malloc(sizeof(char*) * files_int); 
for(i=1; i <= files_int ; i++) 
     { 
      files[i] = malloc(500); 
      //fill with data... 
     } 
//call function with needs the double pointer 
functionA(files); 

//free first array 
for(x=1; x <= max_files ; x++){ 
    free(files[x]); 
    } 
//free second array 
free(files); 

我總是得到glibc檢測到雙免費或腐敗(出)錯誤。

我在做什麼錯?

+7

'files [files_int]'不存在。你忽略了'files [0]' – pmg

+1

你應該爲你的for語句執行'for(int x = 0; x

+0

您正在使用基於1的索引。你應該使用基於0的。目前你正在脫離陣列的末尾。 – Dave

回答

3

數組中C是基於0

for(i=1; i <= files_int ; i++) 
    files[i] = malloc(500); 

導致緩衝區溢出,可能損壞存儲器分配系統,並且不分配files[0]