释放动态数组中的指针

发布于 2024-12-02 01:15:10 字数 2152 浏览 4 评论 0原文

继我之前的问题之后:

从指向字符串的指针

我现在尝试将复制的字符串添加到动态数组中,该数组的大小将根据 SD 卡上的文件数量逐渐增加,并且一旦卡被重新创建被交换了以某种方式进行/更改。

此代码第一次运行良好。当SD卡的内容改变后,调用reReadSD()函数并释放fileList。读取 SD 卡的新内容并将新值写入文件列表,但是,在打印文件列表中的名称时,我得到的是符号而不是正确的名称。我认为这是释放 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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

划一舟意中人 2024-12-09 01:15:10

就我而言,您在数据段中声明了 fileList 指针。所以它的初始值是NULL。当你重新分配时,它的作用就像malloc一样。但是当你释放它时,它仍然指向某个地方并且重新分配失败。您可能应该设置 fileList = NULL 才能生存。

希望这有帮助。

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.

深海里的那抹蓝 2024-12-09 01:15:10

我在这段代码中看不到任何明显的错误,除了如果 free(fileList) 调用标准库 free 函数,您只是释放数组分配的内存指针,而不是其元素指向的单个字符串,因此存在内存泄漏。相反,您需要在释放数组本身之前遍历数组并依次释放每个元素。但这并不能解决你的直接问题。

我能推荐的最好方法是调试代码,或者在关键位置放入调试打印输出,以跟踪程序内部实际发生的情况。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文