【问题标题】:Error free(): Invalid next size (fast)无错误():下一个大小无效(快速)
【发布时间】:2016-11-21 23:12:30
【问题描述】:

我有一个非常简单的程序,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 ***

【问题讨论】:

    标签: c malloc free dynamic-memory-allocation


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      你应该写:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-23
        • 1970-01-01
        • 2012-10-25
        • 2012-05-22
        • 1970-01-01
        • 2017-03-28
        • 2018-05-20
        相关资源
        最近更新 更多