c - 第二次调用后重新分配失败

标签 c malloc realloc

我正在尝试读取要排序的单词列表,我从一个相当小的数组(10 个元素)开始,然后如果当前容量不是,则希望将数组的大小增加 10足够的。这似乎适用于第一个 realloc,但当我尝试再次调用 realloc 时,我收到了 SIGABRT。我确信这是一件我没有看到的简单事情,但我似乎无法弄清楚。这是我的程序:

int main(int argc, char *argv[]){
    char *string = malloc(100);
    // Array of pointers starting with 100 elements
    char **toSort = malloc(100*sizeof(char *));
    if(toSort == NULL) {
        exit(1);
    }

    for(int i = 0; i < 100; i++) {
        // Each string can be up to 100 characters long
        toSort[i] = malloc(101);
        if(toSort[i] == NULL) {
            exit(1);
        }
    }

    // Get all lines in the file
    int counter = 0;
    int max = 10;
    char *toAdd;


    FILE *txt = fopen("wlist0.txt", "r");


    while(fgets ( string, 100, txt ) && counter < max) {;

        toAdd = malloc(100);
        if(toAdd == NULL) {
            exit(1);
        }
        strcpy(toAdd, string);

        toSort[counter] = string;
        counter++;

         //if the array needs to be enlarged
        if(counter == max) {
            char **new = realloc(toSort, (max+10) * sizeof(char));
            if(new == NULL) {
                exit(1);
            }
            for(int i = max; i < max + 10; i++) {
                toSort[i] = malloc(101);
                if(toSort[i] == NULL) {
                    exit(1);
                }
            }
            toSort = new;
            max += 10;
        }
    };

    for(int i = 0; i < max; i++) {
        char *word = toSort[i];
        printf("%s", word);
    }

    for(int i = 0; i < max; i++) {
       free(toSort[i]);
    }

    free(toSort);


    return 0;
};

就像我的评论所说,我的字符串的最大长度为 100 个字符。我想我也可以为字符串动态分配内存,但是当我让其他重新分配工作时我会担心这一点。任何帮助将不胜感激。

最佳答案

realloc() 释放/修改它指向的内存后,此代码将值分配给 toSort:

     //if the array needs to be enlarged
    if(counter == max) {
        char **new = realloc(toSort, (max+10) * sizeof(char));
        if(new == NULL) {
            exit(1);
        }
        for(int i = max; i < max + 10; i++) {
            toSort[i] = malloc(101);  <--- toSort is invalid here
            if(toSort[i] == NULL) {
                exit(1);
            }
        }
        toSort = new;
        max += 10;
    }

这样效果会更好:

     //if the array needs to be enlarged
    if(counter == max) {
        char **new = realloc(toSort, (max+10) * sizeof( *new )); <-- fixed here, too
        if(new == NULL) {
            exit(1);
        }
        toSort = new;
        for(int i = max; i < max + 10; i++) {
            toSort[i] = malloc(101);
            if(toSort[i] == NULL) {
                exit(1);
            }
        }
        max += 10;
    }

您的代码中可能存在其他错误。我还没有完全检查过。

关于c - 第二次调用后重新分配失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50087402/

相关文章:

c - 如何在不取消引用的情况下确定指向数据的大小?

C18伪变量

c - c中的windows异步串口通信

c - 在字符指针数组中打印元素时未确定的行为?

c - 为什么对结构使用 malloc 时会出现段错误 11?

c - 循环中 realloc() 中的段错误

c - 释放字符串数组的函数

ios - 已释放对象的校验和不正确 - 如何在设备上进行故障排除?

c - 重新分配() : invalid next size

C - 使用内部已存在的指针扩展结构数组