错误 free():下一个尺寸无效(快速)

Error free(): Invalid next size (fast)

我有一个非常简单的程序,malloc() 的一些数据,初始化它,然后释放它,我不断收到这个错误。我不知道为什么会这样,有什么想法吗?

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

int main(){
    int * big_mem;
    int row_size = 4096; //not sure about this....
    int num_rows = 5;
    int const_val = 0xFFFFFFFF;

    big_mem = (int *) malloc(row_size * num_rows);
    if(!big_mem){
        printf("Failed to malloc, exiting...");
        return 0;
    }

    i = 0;  
    for(; i < row_size * num_rows; ++i){
        big_mem[i] = const_val;
    }
    printf("Initialized mem, ");

    free(big_mem);
    big_mem = NULL;

    return 0;
}

输出:

*** Error in `./test': free(): invalid next size (fast): 0x0000000000819010 ***

你应该写:

big_mem = (int *) malloc(row_size * num_rows * sizeof(int));

使用malloc(row_size * num_rows * sizeof(int)) .

您没有 malloc 足够的内存,因此您的循环写入了您的 malloc()ed 内存。我很惊讶你不只是遇到了分段错误。