2015-05-16 101 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]