【问题标题】:Copying a string from pointer to string into a dynamic array将字符串从指针复制到字符串到动态数组中
【发布时间】:2011-08-24 15:49:26
【问题描述】:

继我之前的一个问题之后:

Copying a string from a pointer to a string

我现在正在尝试将复制的字符串添加到一个动态数组中,该数组的大小将根据 SD 卡上的文件数量逐渐增加,并且在换出卡后将被删除。

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

            // Things are looking good so far
            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;

【问题讨论】:

    标签: arrays dynamic


    【解决方案1】:

    Realloc 返回一个指向重新分配的内存块的指针,它可能与 ptr 参数相同或一个新位置。因此,调用:

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

    如果在新位置分配内存,则不会更新fileList 指针。新文件列表的地址实际上存储在_tmp 中,因此您必须执行以下操作:

    fileList = (TCHAR *) _tmp;
    

    在您致电realloc之后。

    另外,我不太明白

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

    这不总是做同样的事情吗?

    【讨论】:

    • 非常感谢,我没有发现。同意第二点,想法是在块中分配内存if (files_allocated==0) files_allocated=5; //initial allocation else files_allocated+=5; //more space needed
    猜你喜欢
    • 2020-11-04
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 2010-09-27
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多