C - 我认为我做错误重新分配

时间:2016-01-04 13:21:02

标签: c memory-management

我正在做一些练习以准备我的测试,其中一个我必须从数组中删除重复的int值:

int *eliminaDup(int *vect, int dim, int *dim_nodup){
    int i = 0, newDim = 0, found = 0, j = 0;
    int *tmpArr = malloc(dim * sizeof(int));
    for(i = 0; i < dim; i++){
        j = 0; found = 0;
        while(j < newDim && !found){
            if(vect[i] == tmpArr[j])
                found = 1;
            j++;
        }
        if(!found){
            tmpArr[newDim] = vect[i];
            newDim++;
        }
    }
    *dim_nodup = newDim;
    return (realloc(tmpArr, newDim * sizeof(int)));
}

在main方法中这样称呼:

nodup=eliminaDup(input,dim,&dim_nodup);
printf("Print of the new Array: (%d values)\n", dim_nodup);
for (i=0; i<dim_nodup; i++){
    printf("%d\n",nodup[i]);
}

但是当我尝试执行代码时,This happens

ARRAY IN INUT输入:

 [1;2]

输出:

1
2

输出预期:

1
2

...other output from the code...

正如您在屏幕上看到的那样,代码应该继续并打印其他内容。

我做了一些尝试,我看到了代码&#34;锁定&#34;正好在印刷品之后,但它从未出现过。

...为什么?我正在敲击键盘。

编辑:完成计划

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

int *leggiInput(int *dim);
int *eliminaDup(int *vect, int dim, int *dim_nodup);
int ugualeASomma(int *vect,int dim);
int *maggioreDeiSuccessivi(int *vect, int dim);

int main()
{
    int *input, *nodup, *results;
    int dim, dim_nodup, i;

    //Legge l'input
    input=leggiInput(&dim);
    printf("Stampa dei valori in input: (%d valori)\n", dim);
    for (i=0; i<dim; i++)
        printf("%d\n",input[i]);

    //Elimina i duplicati
    nodup=eliminaDup(input,dim,&dim_nodup);
    printf("Stampa dei valori senza duplicati: (%d valori)\n", dim_nodup);
    for (i=0; i<dim_nodup; i++){
        printf("%d\n",nodup[i]);
    }
    //Esegue ugualeASomma
    printf("Risultato di ugualeASomma: %d\n", ugualeASomma(nodup,dim_nodup));

    //Esegue maggioreDeiSuccessivi
    results=maggioreDeiSuccessivi(nodup,dim_nodup);
    printf("Risultato maggioreDeiSuccessivi:\n");
    for(i=0; i<dim_nodup; i++)
        printf("%d\n",results[i]);

    return 0;
}

int *leggiInput(int *dim){
    int n, i;
    scanf("%d", &n);
    int *arr = malloc(n * sizeof(int)); 
    for(i = 0; i < n; i++){
        scanf("%d", &arr[i]);
    }

    *dim = n;
    return arr;
}

int *eliminaDup(int *vect, int dim, int *dim_nodup){
    int i = 0, newDim = 0, trovato = 0, j = 0;
    int *tmpArr = malloc(dim * sizeof(int));
    while(i < dim){
        j = 0; trovato = 0;
        while(j < newDim && !trovato){
            if(vect[i] == tmpArr[j])
                trovato = 1;
            j++;
        }
        if(!trovato){
            tmpArr[newDim] = vect[i];
            newDim++;
        }
    i++;
    }
    *dim_nodup = newDim;
    return (realloc(tmpArr, newDim * sizeof(int)));
}

int ugualeASomma(int *vect, int dim){
    int somma = 0, i = 0, j, trovato = 0;

    while(i < dim)
        somma += vect[i];
    while(i < dim){
        if(vect[i] == somma - vect[i])
            trovato = 1;
    }

    return trovato;
}

int *maggioreDeiSuccessivi(int *vect, int dim){
    int i = 0, j, trovato;
    while(i < dim){
        j = i+1; trovato = 0;       
        while(j < dim && !trovato){
            if(vect[i] <= vect[j])
                trovato = 1;
            else
                j++;        
        }

        if(trovato) vect[i] = 0;
        else vect[i] = 1;
        i++;                
    }

    return vect;
}

编辑:在注释中将malloc更改为calloc。

2 个答案:

答案 0 :(得分:2)

当realloc失败时,它返回NULL,所以你应该检查一下并返回tmpArr:

int* p = realloc(tmpArr, newDim * sizeof(int));
return p != NULL ? p : tmpArr; 

最好初始化所有声明的变量,即使它们稍后会被初始化。您可能稍后忘记它并假设它随着函数的增长而初始化。

你这里有一个无限循环

int ugualeASomma(int *vect, int dim){
    int somma = 0, i = 0, j, trovato = 0;

    while(i < dim)
        somma += vect[i];
    while(i < dim){
        if(vect[i] == somma - vect[i])
            trovato = 1;
    }

    return trovato;
}

我永远不会增加

答案 1 :(得分:0)

我想说Anders Karlsson有一个好点。这对我来说很好:

int ugualeASomma(int *vect, int dim){
    int somma = 0, i = 0, j, trovato = 0;

    while(i < dim)
    {
        somma += vect[i];
        if(vect[i] == somma - vect[i])
            trovato = 1;
        i++;
    }

    return trovato;
}