Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用特定输入中止Malloc_C_Malloc - Fatal编程技术网

使用特定输入中止Malloc

使用特定输入中止Malloc,c,malloc,C,Malloc,这是我第一次在这里发帖,如果我没有遵守正确的礼仪,我深表歉意。我也试着寻找答案,但没有结果 基本上,我有一个贪心硬币兑换算法的函数,它接受一些int作为输入。在我的函数中,我返回一个包含每个硬币的malloc'd数组。虽然它在很大程度上起作用,但出于某种原因,任何产生5,9。。。(+4)个硬币作为最佳分配,即9个硬币为(5+1+1+1),或650个硬币为13个,每个50个,导致程序中止,并显示以下消息: hello: malloc.c:2401: sysmalloc: Assertion `(o

这是我第一次在这里发帖,如果我没有遵守正确的礼仪,我深表歉意。我也试着寻找答案,但没有结果

基本上,我有一个贪心硬币兑换算法的函数,它接受一些int作为输入。在我的函数中,我返回一个包含每个硬币的malloc'd数组。虽然它在很大程度上起作用,但出于某种原因,任何产生5,9。。。(+4)个硬币作为最佳分配,即9个硬币为(5+1+1+1),或650个硬币为13个,每个50个,导致程序中止,并显示以下消息:

hello: malloc.c:2401: sysmalloc: Assertion `(old_top == initial_top (av) && 
old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse 
(old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted (core dumped)
然而,每一个不是5或5+4+的硬币分布。。。作品不知道该怎么办

这就是功能:

int* greedyAlg(int value)//computes and returns the optimal (minimum) number of denominations for a given value for US currency
    {
        //denominations
        int one = 1, five = 5, ten = 10, twenty = 20, fifty = 50;
        int x5 = 0, x4 = 0, x3 = 0, x2 = 0, x1 = 0;
        int count = 0;
        //int[] denom;

    while(value != 0)
    {
        if(value >= fifty)
        {
            value -= fifty;
            count++;
            x5++;

            /*while(value >= fifty)
            {
                // int *i = &fifty;
                value-=fifty;
                count++;
                x5++;
            }*/
        }
        else if(value < fifty && value >= twenty)
        {
            value -= twenty;
            count++;
            x4++;
        }
        else if(value < twenty && value >= ten)
        {
            value -= ten;
            count++;
            x3++;
        }
        else if(value < ten && value >= five)
        {
            value -= five;
            count++;
            x2++;
        }
        else if(value < five && value >= one)
        {
            value -= one;
            count++;
            x1++;
        }
    }
    //printf("Optimal denominations: ");
    int* denom = malloc(sizeof(int)*(count + 1));
    //int* denom = (int *)calloc(count + 1,sizeof (int));

    denom[0]=count;
    for(int i = 1; i<= (x5 + 1); i++){
        denom[i] = fifty;
    }
    for(int i= (x5 + 1); i<=(x5 + x4) + 1; i++){
        denom[i] = twenty;
    }
    for(int i = (x5 + x4) + 1; i <= ( x5 + x4 +x3 ) + 1; i++){
        denom[i] = ten;
    }
    for(int i = (x5 + x4 + x3) + 1; i <= (x5 + x4 + x3 + x2) + 1; i++){
        denom[i] = five;
    }
    for(int i = (x5 + x4 + x3 + x2) + 1; i <= (x5 + x4 + x3 + x2 + x1) + 1; i++){
        denom[i]=one;
    }
    return denom;
    free(&denom);
    //return count;
}
(在我的代码中,我设置了一个循环,使用x作为测试的用户输入)


如果需要,我可以发布任何其他相关详细信息。

假设
计数等于
x5+x4+x3+x2+x1
,则您得到了一个错误:

for(int i=(x5+x4+x3+x2)+1; i<=(x5+x4+x3+x2+x1)+1; i++){

free()
将永远不会执行,而且如果将
&
放在其他位置,则应将其从
denom
之前删除。

我尚未深入查看代码,但乍一看,在返回算法函数后,它看起来好像是在释放。您可能想先解决这个问题。您好,谢谢您的输入,但是将循环条件更改为<,正如另一位评论员所建议的那样有效。但是我仍然不确定从何处释放数组。当询问有关运行时问题的问题时,正如这个问题所做的那样,请发布一个帖子,以便我们可以重现该问题,而不必猜测您的实际代码是什么样子的这就是问题所在!非常感谢。我不确定放在哪里,但这是有道理的。
printGreedyArr(greedyAlg(x),greedyAlg(x)[0]);
for(int i=(x5+x4+x3+x2)+1; i<=(x5+x4+x3+x2+x1)+1; i++){
for(int i=(x5+x4+x3+x2)+1; i<(x5+x4+x3+x2+x1)+1; i++){
return denom;
free(&denom);