Following on from a previous question I had here :
Copying a string from a pointer to a string
I'm now trying to add the copied string into a dynamic array, which will gradually increase in size depending on the number of files on the SD card and will be recreated once the card is swapped out/changed in some way.
This code works fine the first time around. After the contents of the SD card are changed, the reReadSD() function is called and the fileList is freed. The new contents of the SD card are read and the new values written to the fileList, however, on printing out the names from the fileList, I am getting symbols rather than the proper names. I assume this is a mistake in freeing up the fileList and reinitializing it as the same block of code works on system power up (when reReadSD is called for the first time) but not the second time it is called. Can anyone shed any light on this?
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
and..
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;
}
fileLi开发者_C百科st = _tmp;
}//end num_files==files_allocated
fileList[num_files] = item;
num_files++;
}//end AddToArray
with..
TCHAR **fileList;
u32 num_files=0;
u32 files_allocated=0;
As far as I am concerned you declared fileList pointer in data segment. So it has NULL initial value. And when you realloc it just acts like malloc. But when you free it up it still points somewhere and realloc fails. You should probably set fileList = NULL order to survive.
Hope this helps.
I can't see any obvious mistake in this code, except that if free(fileList)
calls the standard library free
function, you are only freeing the memory allocated by the array of pointers, not the individual character strings its elements point to, so you have a memory leak. Instead, you need to iterate through the array and free each element in turn, before freeing the array itself. But this does not solve your direct problem.
The best I can recommend is debugging the code, or putting in debug printouts in critical places, to follow what is actually happening inside the program.
精彩评论