【发布时间】:2011-08-26 07:48:13
【问题描述】:
继我之前的一个问题之后:
Copying a string from a pointer to a string
我现在正在尝试将复制的字符串添加到一个动态数组中,该数组的大小将根据 SD 卡上的文件数量逐渐增加,并且将在换出/换入卡后 重新创建某种方式。
此代码第一次运行良好。 SD卡内容改变后,调用reReadSD()函数,释放fileList。读取 SD 卡的新内容并将新值写入 fileList,但是,在从 fileList 打印出名称时,我得到的是符号而不是正确的名称。我认为这是释放 fileList 并重新初始化它的错误,因为相同的代码块在系统启动时工作(当第一次调用 reReadSD 时)但不是第二次调用它。任何人都可以对此有所了解吗?
void reReadSD()
{
free(fileList);
files_allocated=0;
num_files=0;
reRead_flag=0;
if(f_mount(0, &fatfs ) != FR_OK ){
/* efs initialisation fails*/
}//end f_mount
FRESULT result;
char *path = '/'; //look in root of sd card
result = f_opendir(&directory, path); //open directory
if(result==FR_OK){
for(;;){
result = f_readdir(&directory, &fileInfo); //read directory
if(result==FR_OK){
if(fileInfo.fname[0]==0){break;} //end of dir reached escape for(;;)
if(fileInfo.fname[0]=='.'){continue;} //ignore '.' files
TCHAR* temp;
temp = malloc(strlen(fileInfo.fname)+1);
strcpy(temp, fileInfo.fname);
AddToArray(temp);
}//end read_dir result==fr_ok
}//end for(;;)
}//end open_dir result==fr_ok
}//end reReadSD
和..
void AddToArray (TCHAR* item)
{
u32 delay;
if(num_files == files_allocated)
{
if (files_allocated==0)
files_allocated=5; //initial allocation
else
files_allocated+=5; //more space needed
//reallocate with temp variable
void *_tmp = realloc(fileList, (files_allocated * sizeof(TCHAR*)));
//reallocation error
if (!_tmp)
{
LCD_ErrLog("Couldn't realloc memory!\n");
return;
}
fileList = _tmp;
}//end num_files==files_allocated
fileList[num_files] = item;
num_files++;
}//end AddToArray
与..
TCHAR **fileList;
u32 num_files=0;
u32 files_allocated=0;
【问题讨论】: