我正在尝试为 char 数组释放内存空间:
static void cleanArray(char* array)
{
int i;
int size = strlen(array) + 1;
for(i = 0; i < size; i++)
free(array[i]);
free(array);
}
int main(){
char* array = (char *)malloc(1 * sizeof(char*));
int c;
for(c=0;c<100;c++){ //code to populate some string values in the array.
void *p = realloc(array, (strlen(array)+strlen(message)+1)*sizeof(char*));
array = p;
strcat(array, "some string");
}
cleanArray(array); //I get error only when I call this method.
}
但是对于上面的代码,我遇到了Segmentation fault
错误。
当我只是尝试以下代码而不是 cleanArray() 我不认为数组被释放:
free(array);
printf("%s",array); //it prints all the values in the array. Hence, I concluded it is not freedup.