如何解决此malloc断言错误?

时间:2018-11-09 15:31:54

标签: c malloc assertion

当我尝试在GitLab上使用管道时,管道给了我这个错误:

task2:malloc.c:2394:sysmalloc:断言`(old_top == initial_top(av)&& old_size == 0)|| (((无符号长)(old_size)> =最小大小&& prev_inuse(old_top)&&((无符号长)old_end&(页面大小-1))== 0)'失败。

这是我的代码,用于计算素数的代码:

int main (int argc, char *argv[])
{
    int *in = NULL, input2, input1, i, j, x, diff;

    /* if (isdigit(in1) == 0 || isdigit(in2) == 0)
    {
        printf("Please input numbers.\n");
        return 1;
    } */

    if (argc != 3)
    {
        printf("Please enter 2 arguments.\n");
        return 1;
    }

    input1 = strtol(argv[1], NULL, 10);
    input2 = strtol(argv[2], NULL, 10);

    diff = (input2 - input1);
    in = (int *) malloc (diff * sizeof(int));
    for (x = 0; x <= diff; x++)
        in[x] = 1;


    if (input1 >= 1000000 || input2 > 1000000 || input1 < 0 || input2 < 0 || input1 >= input2)
    {
        printf("Please input a number from 0-1000000, and input 1 has to be a smaller number than input2.\n");
        return 1;
    }

    for (i = 0; i <= diff; i++) 
    {
        x = input1+i;
        for (j = 2; j < x; ++j)
        {
            if(x % j == 0)
            {
                in[i] = 0;
                break;
            }
        }
        if (in[i] == 1 && x != 1)
        printf("%d\n", x);
    }
    free(in);
    return 0;
}

有人能帮助我吗? 预先感谢

编辑:添加了免费的(in); 仍然会收到错误。

0 个答案:

没有答案