Malloc 抛出一个错误,以释放指针

Malloc is throwing an error for pointer being freed

提问人:cobb208 提问时间:6/11/2022 最后编辑:cobb208 更新时间:6/11/2022 访问量:72

问:

所以我有一个函数来创建一个灵活的数组类型,所有路径都使用 calloc 来定义数组的大小。但是,当我尝试销毁结构时,它说内存未分配。

typedef struct {
    int size;
    void *items;
} Array;

typedef enum {
    CHAR,
    INT,
    FLOAT,
    DOUBLE
} datatype;

Array *create_array(int initial_size, datatype type)
{
    Array *arr = malloc(sizeof(Array));

    arr->size = initial_size;

    switch (type) {
        case CHAR:
            arr->items = (char *) calloc(5, sizeof(char));
            break;
        case INT:
            arr->items = (int *) calloc(arr->size, sizeof(int));
            break;
        case FLOAT:
            arr->items = (float *) calloc(arr->size, sizeof(float));
            break;
        case DOUBLE:
            arr->items = (double *) calloc(arr->size, sizeof(double));
            break;
        default:
            exit(1); // will handle later on
            break;
    }
    return arr;
}

void destroy_array(Array *arr)
{
    free(arr->items); // this is the line that throws the error
    free(arr);
}

确切的错误是:

flexibleArray(63110,0x10c3c6600) malloc:对象 0x600000c21120 的 *** 错误:未分配正在释放的指针 flexibleArray(63110,0x10c3c6600) malloc: *** 在要调试的malloc_error_break中设置断点

我只是不明白为什么我用 calloc 分配的内存不需要释放。

感谢任何人的投入!

主环路

里面有很多随机代码,我用它们来测试如何改变数组的大小。我认为这不会影响销毁函数的结果。

    int main() {
        Array *newArr = create_array(5, INT);
        int *val;
        int w = 20;
        val = &w;

        printf("%s\n", typename(w));

        ((int *)newArr->items)[0] = *val;
        ((int *)newArr->items)[1] = 35;
        ((int *)newArr->items)[2] = 20;
        ((int *)newArr->items)[3] = 351;
        ((int *)newArr->items)[4] = 315;

        for(int i = 0; i < newArr->size; i++) {
            printf("%d\n", ((int *)newArr->items)[i]);
        }

        realloc(newArr->items, 10);

        ((int *)newArr->items)[5] = 50;
        for(int i = 0; i < newArr->size; i++) {
            printf("%d\n", ((int *)newArr->items)[i]);
        }

        destroy_array(newArr);
    }
C 内存 结构 malloc calloc

评论

0赞 Ted Lyngmo 6/11/2022
无关:这个案子看起来很不对劲。为什么它为 5 而不是分配内存?您也不需要转换 you get from to 等。在这种情况下,它尤其毫无意义,因为它本身就是一个 .CHARchararr->sizecharvoid*callocint*arr->itemsvoid*
1赞 Retired Ninja 6/11/2022
realloc返回一个指针,说明原因。
1赞 Ted Lyngmo 6/11/2022
realloc(newArr->items, 10);< - 这个 - 不要忽略返回值

答:

0赞 cobb208 6/11/2022 #1

感谢退休的忍者和泰德·林格莫!

在 realloc 文档上没有看到 * 以查看它返回了指向新对象的指针。用:

...

newArr->items = realloc(newArr->items, 10);
...

删除了该错误。

感谢您的帮助!

Ted Lyngmo 的另一条评论的旁注是我正在玩的泛型。它被宣布只是没有把它放在这里......

评论

0赞 Ted Lyngmo 6/11/2022
这只是问题的一部分
0赞 cobb208 6/11/2022
我敢肯定,它充斥着各种各样的问题和我没有正确做的事情。慢慢地学习 C 语言有很多内容。
0赞 cobb208 6/11/2022
我假设您之前对 realloc 的评论,我应该使用size_t值(如 sizeof(int))而不是普通的 int 值。
1赞 Ted Lyngmo 6/11/2022 #2

当前忽略 的返回值。如果它需要重新定位分配的数据,则会出现未定义的行为。realloc

另一个问题是你为字节分配空间,而不是 .1010int

修复:

// allocate space for 10 `int`:
void* np = realloc(newArr->items, 10 * sizeof(int));

if(np) {
    // realloc success
    newArr->items = np;
} else {
    // realloc failure
    destroy_array(newArr);
    return 1;
}