0

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;

1 Answer 1

0

Realloc returns a pointer to the reallocated memory block, which may be either the same as the ptr argument or a new location. Therefore, calling:

void *_tmp = realloc(fileList, (files_allocated * sizeof(TCHAR*)));

will not update the fileList pointer if the memory is allocated in a new place. The address of the new file list is actually stored in _tmp, so you would have to do something along the lines of:

fileList = (TCHAR *) _tmp;

after you call realloc.

Also, I don't quite understand the meaning of

if (files_allocated == 0)
     files_allocated = 1; //initial allocation
else
     files_allocated++; //more space needed 

Doesn't this always do the same thing?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, I had not spotted that. Agreed on the second point, the idea was to allocate mem in blocks if (files_allocated==0) files_allocated=5; //initial allocation else files_allocated+=5; //more space needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.