69,371
社区成员
发帖
与我相关
我的任务
分享
char **input_files =(char **)malloc(sizeof(char *)*1);
int counter=0;
char file[]="Hello World!!!";
for(counter=0; counter <10; counter++)
{
if(counter==0)
input_files=(char **)malloc(sizeof(char *)*(counter+1));
else
input_files=(char **)realloc(input_files,sizeof(char *)*(counter+1));
char *temp=NULL;
temp=(char *)malloc(sizeof(char)*(strlen(file)+1)); //strlen(file)+1 == 15
strcpy(temp,file);
input_files[counter]=temp;
}
clean_files1(1,input_files);//再test_clean.c文件中,在test_clean.h中已经定义了这个函数
再test_clean.c中写入
int in_file_counter=0;
for(in_file_counter=0; in_file_counter <size; in_file_counter++)
{
printf("%s\n",files[in_file_counter]);
free(files[in_file_counter]);
files[in_file_counter] = NULL; //add this
printf("%s\n",files[in_file_counter]);//依然存在,并没有清空.
}
free(files);
files=NULL;
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void del_memory(char **p) // 释放空间
{
int i;
for (i=0; i<10; i++)
{
printf("%s",*(p+i));
free(*(p+i));
}
}
void main()
{
// char **input_files=(char **)malloc(sizeof(char *)*1); 这不用申请内存吧
char **input_files;
int counter=0;
char file[]="Hello World!!!";
char * temp =NULL; //这里改动了
for(counter=0; counter <10; counter++)
{
if(counter==0)
input_files=(char **)malloc(sizeof(char *)*(counter+1));
else
input_files=(char **)realloc(input_files,sizeof(char *)*(counter+1));
temp = (char *)malloc(sizeof(char)*(strlen(file)+1));
strcpy(temp,file);
input_files[counter]=temp;
/*input_files[counter]指向temp的内容,
故不能清除temp的内存空间,
待会儿用input_files[counter]来释放*/
}
del_memory(input_files); //释放空间
}
char **input_files=(char **)malloc(sizeof(char *)*1);