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 deleted once the card is swapped out.
EDIT: 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). Can anyone shed any light on this?
EDIT: updated code
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;
}
// Things are looking good so far
fileList = _tmp;
}//end num_files==files_allocated
fileList[num_files] = item;
num_files++;
}//end AddToArray
Where
TCHAR **fileList;
u32 num_files=0;
u32 files_allocated=0;