c - Malloc 中止特定输入

标签 c malloc

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

基本上,我有一个贪婪硬币找零算法的函数,它以一些 int 作为输入。在我的函数中,我返回一个包含每个硬币的 malloc 数组。虽然它在大多数情况下都有效,但出于某种原因,任何产生 5、9、... (+4) 个硬币作为最佳分配的硬币值(value),即 9 将是 (5 + 1 + 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;
}

这就是我打印它的方式:

//prints elements of array created by (greedy) coin change algorithm
void printGreedyArr(int arr[], size_t count)
{
    for(int i = 1; i <= count; i++)
    {
        printf("%s%d%s, ","[",arr[i],"]");
    }
    printf("\n%s %d\n","count was",count);
}

我用第 0 个索引来调用它,它包含这样的长度:

printGreedyArr(greedyAlg(x),greedyAlg(x)[0]);

(在我的代码中,我设置了一个循环,将 x 作为用户输入进行测试)

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

最佳答案

假设count等于 x5+x4+x3+x2+x1 ,你有一个差一个错误:

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++){

其他for也类似循环。请注意,终止条件已从 <= 更改为至 < .

还有:

return denom;
free(&denom);

那个free()永远不会被执行,还有&应该从 denom 之前删除如果你把它放在别处。

关于c - Malloc 中止特定输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52213683/

相关文章:

c - 在C语言中,如何在单独的变量中使用多个字符串

c++ - C++ 中 undefined reference

使用 malloc 的连续内存块

c++ - 什么情况下 std::vector.clear() 会调用析构函数?

在 C 编程中组合文本

使用 python 循环的每次迭代创建一个文本文件

c - 如何在函数内重新分配数组而不丢失数据? (在 C 中)

c - 在结构中打印结构中数组的成员?

c - 使用 LD_PRELOAD 机制覆盖 'malloc'

C程序中裁剪BMP文件图像