Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6365229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:16:02+00:00 2026-05-25T00:16:02+00:00

Following on from a previous question I had here : Copying a string from

  • 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 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;
        }

        fileList = _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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:16:03+00:00Added an answer on May 25, 2026 at 12:16 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from a previous question, I'm trying to clean up some data where
Following on from my previous question , I have now managed to rotate my
Following on from a previous question, shown here , I was wondering how I
Following on from my previous question, Python time to age , I have now
Following on from my previous question , is it possible to make a Python
Following on from my previous question [link text][1] , I have a new problem.
Following up from my previous question , why is CShell so different from C?
Following on from a previous question, for some reason when I use the following
Following on from a previous question relating to heap usage restrictions , I'm looking
Following on from a previous question , I am creating a symbolic link on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.