相关疑难解决方法(0)

Realloc没有调整指针数组的大小

我继续传递并返回dirs_later_array.当我在else块中找到"new_size = ..."时,我第二次得到new_size为2.到现在为止还挺好.但是当我做一个realloc

dirs_later_array = realloc(dirs_later_array,
new_size * sizeof(struct dirs_later*));
Run Code Online (Sandbox Code Playgroud)

对于dirs_later_array,sizeof保持为4,即指针的大小.我能够成功地存储在dirs_later_array [1],但是下次进入函数时该值会被覆盖.

struct dirs_later** add_struct(const char *findme, struct dirent *dptr,
        struct stat *this_lstat, char *relative_path, const char *type_str,
        struct dirs_later **dirs_later_array) {

    struct dirs_later *new_dir = malloc(sizeof(struct dirs_later));
    check_realloc_dirs_error(new_dir);

    if (strcmp(dptr->d_name, ".")) { //Dir and not same directory
        //Copy the relative path to the struct
        char *relative_path2;
        relative_path2 = malloc(strlen(relative_path) + 1);
        check_realloc_error(relative_path2);
        strcpy(relative_path2, relative_path);

        //if (strlen(relative_path) > 0)
        //    relative_path2[strlen(relative_path) - 1] = '\0';

        if (NULL != new_dir) { …

c

2
推荐指数
2
解决办法
2272
查看次数

标签 统计

c ×1