Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

c - Realloc is not resizing array of pointers

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] = '';

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



See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...