不能释放双指针

时间:2015-05-16 15:55:09

标签: c pointers

我的问题是我必须释放一个双指针。我已经尝试过了:

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中检测到双重释放或损坏(out)错误。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

C中的数组基于0

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

导致缓冲区溢出,可能破坏内存分配系统,并且您不分配files[0]

相关问题