ReAlloc如果不调整指针数组 [英] Realloc is not resizing array of pointers

查看:185
本文介绍了ReAlloc如果不调整指针数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直传递和返回dirs_later_array。当我到了new_size = ...中的其他块,我结束了2 new_size第二次左右。到现在为止还挺好。但是,当我做的realloc

  dirs_later_array = realloc的(dirs_later_array,
new_size *的sizeof(结构dirs_later *));

sizeof的保持在图4中,指针的大小,为dirs_later_array。我能够在dirs_later_array [1],以成功地存储,但该值不断被覆盖掉,下一次我进入的功能。


结构dirs_later ** add_struct(为const char * findme,结构的dirent * DPTR,
        结构统计* this_lstat,字符* RELATIVE_PATH,为const char * type_str,
        结构dirs_later ** dirs_later_array){    结构dirs_later * NEW_DIR =的malloc(sizeof的(结构dirs_later));
    check_realloc_dirs_error(NEW_DIR);    如果(STRCMP(dptr-> d_name,。)){//目录,而不是同一个目录
        //相对路径复制到结构
        字符* relative_path2;
        relative_path2 = malloc的(strlen的(RELATIVE_PATH)+1);
        check_realloc_error(relative_path2);
        的strcpy(relative_path2,RELATIVE_PATH);        //如果(strlen的(RELATIVE_PATH)> 0)
        // relative_path2 [strlen的(RELATIVE_PATH) - 1] ='\\ 0';        如果(NULL!= NEW_DIR){
            new_dir-> findme = findme;
            new_dir-> DPTR = DPTR;
            new_dir-> ST_MODE = this_lstat-> ST_MODE;
            new_dir-> RELATIVE_PATH = relative_path2;
            new_dir-> type_str = type_str;
        }
        INT new_size = 0;
        / *
         //检查,如果这是在结构中的第一个元素
         如果(sizeof的(dirs_later_array)/的sizeof(结构dirs_later *)== 1){
         new_size = 1;
         }
         * /
        如果(dirs_later_array == NULL){
            dirs_later_array =的malloc(sizeof的(结构dirs_later *)); //存储目录结构或过程的后期
            check_realloc_arr_error(* dirs_later_array);
            new_size = 1;
        }其他{            //将目录添加到目录阵列
            new_size =(((的sizeof(dirs_later_array)+的sizeof(结构dirs_later *)))/的sizeof(结构dirs_later *));
            //输出(新大小:%D,new_size);
        }
        dirs_later_array =的realloc(dirs_later_array,
                new_size *的sizeof(结构dirs_later *));
        check_realloc_arr_error(dirs_later_array);
        dirs_later_array [new_size - 1] = NEW_DIR;
    }
    返回dirs_later_array;
}


解决方案

运营商的sizeof 是一个编译时功能,它只检查一个前$ P $的静态大小pssion。因此,对于指针它只返回指针是4平台上的大小。 的sizeof 并不衡量一个动态分配的数据的大小。在C中没有标准的功​​能来获取动态分配数据的大小。

I keep passing in and returning the dirs_later_array. When I get to "new_size=..." in the else block, I end up with new_size of 2 the second time around. So far so good. But when I do a realloc

dirs_later_array = realloc(dirs_later_array,
new_size * sizeof(struct dirs_later*));

the sizeof remains at 4, the size of the pointer, for dirs_later_array. I'm able to succesfully store at dirs_later_array[1] but that value keeps getting overwritten the next time I go into the function.

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) {
            new_dir->findme = findme;
            new_dir->dptr = dptr;
            new_dir->st_mode = this_lstat->st_mode;
            new_dir->relative_path = relative_path2;
            new_dir->type_str = type_str;
        }
        int new_size = 0;
        /*
         //Check if this is the first element in the struct
         if (sizeof(dirs_later_array) / sizeof(struct dirs_later*) == 1) {
         new_size = 1;
         }
         */
        if (dirs_later_array == NULL) {
            dirs_later_array = malloc(sizeof(struct dirs_later*)); //Store the directory structures or process later
            check_realloc_arr_error(*dirs_later_array);
            new_size = 1;
        } else {

            //Add directories to directories array
            new_size = (((sizeof(dirs_later_array) + sizeof(struct dirs_later*)))/sizeof(struct dirs_later*));
            //printf("new size: %d",new_size);
        }
        dirs_later_array = realloc(dirs_later_array,
                new_size * sizeof(struct dirs_later*));
        check_realloc_arr_error(dirs_later_array);
        dirs_later_array[new_size - 1] = new_dir;
    }
    return dirs_later_array;
}



解决方案

Operator sizeof is a compile time feature and it only checks the static size of an expression. So for pointer it only returns the size of that pointer which is 4 on your platform. sizeof does not measure the size of a dynamically allocated data. There is no standard feature in C to get the size of dynamically allocated data.

这篇关于ReAlloc如果不调整指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆