c - 标记化数组上的realloc():信号SIGABRT错误

Kev*_*lch 5 c pointers realloc dynamic-arrays sigabrt

在第56行,我正在尝试调整数组的大小:

tokenArray = (char**) realloc(tokenArray, tokSize * (sizeof(char)));
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

(11972,0x7fff7ca4f300)malloc:*对象0x100105598的错误:释放对象的校验和不正确 - 对象可能在被释放后被修改.*在malloc_error_break中设置断点以进行调试

这是一个类的编程分配,我已经被特别指示动态分配我的数组,然后根据需要进行扩展.我已经广泛搜索了同样的另一个线程,这个线程对我来说并不太先进,但没有运气......所以希望我能得到一些帮助.谢谢!这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_ROW_SIZE 81

void strInput(char str[], int numElem);


int main(int argc, const char * argv[])
{
    printf("Enter a string of any number of integers separated by spaces or tabs.\n");
    printf("Maximum string length is 80 characters.\n");
    printf("Enter an empty string to conclude input.\n");


    int arrSize = 10, tokSize = 10, i = 0, j = 0;

    char** inputArray = malloc(arrSize * (sizeof(char)));
    char** tokenArray = malloc(tokSize * (sizeof(char)));


    do {
        inputArray[i] = malloc(MAX_ROW_SIZE * sizeof(int));

        strInput(inputArray[i], arrSize);

        if ((inputArray[i][0] != '\0') && (i == (arrSize - 1)))
        {
            arrSize = arrSize * 2;
            inputArray = (char**) realloc(inputArray, arrSize * (sizeof(char)));
        }

        while (inputArray[i][j] != '\0')
        {
            printf("%c", inputArray[i][j]);
            j++;
        }
        j = 0;

        i++;
    } while (inputArray[i-1][0] != '\0');

    i = 0;

    while (inputArray[i][0] != '\0')
    {

        if ((tokenArray[j] = strtok(inputArray[i], " \t")))
            j++;
        while ((tokenArray[j] = strtok(NULL, " \t")))
        {
            if (j == (tokSize - 1))
            {
                tokSize = 2 * tokSize;

 //This is the line where I get the error
                tokenArray = (char**) realloc(tokenArray, tokSize * (sizeof(char)));
            }
            j++;
        }
        i++;
    }

    printf("printing the tokenized arrays: ");
    for (i = 0; i < j; i++)
        printf("%s ", tokenArray[i]);

    free(inputArray);
    free(tokenArray);

    return 0;
}

void strInput(char str[], int numElem)
{


    int j, k = 0;

    j = k;

    while ((str[k] = getchar()) != '\n')
    {
        k++;
    }

    if (str[k] == '\n')
        str[k] = '\0';
}
Run Code Online (Sandbox Code Playgroud)

Que*_*tin 5

1.不要把结果malloc和朋友联系起来.

它充其量是无用的,最坏的是危险的.

2.记住你正在变大的规模malloc.

char** inputArray = malloc(arrSize * (sizeof(char)));
Run Code Online (Sandbox Code Playgroud)

这毫无意义,可能是偶然的.根据经验,您所使用的类型malloc和指向结果存储的指针应该只有一个间接不同.即:

char** inputArray = malloc(arrSize * sizeof(char*));
//  ^^ Double-pointer     vs     Single pointer ^
Run Code Online (Sandbox Code Playgroud)

更好的经验法则,让编译器搞清楚.sizeof可以推导出它应该从表达式中测量的类型.

char **inputArray = malloc(arrSize * sizeof(*inputArray));
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为操作数sizeof是一个未评估的上下文.指针实际上不会被解除引用,只会推导出它的类型.
旁注:sizeof表达式周围不需要括号,但为了清晰起见,我将它们留下了.一旦你感到舒服,将它们移除.

3.检查分配是否成功.

mallocNULL如果遇到麻烦,朋友们会回来.你应该检查一下.

4.修复你的 realloc

inputArray = realloc(inputArray, /*...*/);
Run Code Online (Sandbox Code Playgroud)

这是错的.如上所述,如果realloc失败,它将返回NULL 并且不执行任何其他操作.这意味着inputArray仍然指向其先前的存储.也就是说,直到你用NULL realloc刚才返回的那个特朗普这个指针,然后泄漏说存储.哎呀.

始终存储,检查,然后分配结果realloc.

char **inputArray_ = realloc(inputArray, /*...*/);
if(!inputArray_) {
    /* Allocation failure, handle it and break out */
}
inputArray = inputArray_;
Run Code Online (Sandbox Code Playgroud)