C - 我认为我做错了 realloc

标签 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 :

输入数组:

 [1;2]

输出:

1
2

预期输出:

1
2

...other output from the code...

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

我做了一些尝试,我看到代码恰好在打印后“锁定”,但它从未从 for 中出来。

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

编辑:完整程序

#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 的注释。

最佳答案

当 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;
}

我永远不会递增

关于C - 我认为我做错了 realloc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34592147/

相关文章:

c - 预测 C 代码中的缺陷

c++ - 如何为动态分配的 STL 容器设置分配器?

java - 为什么 PermGen 空间在增长?

Java - 使用 Swing 框架和抽象类防止 Java 堆内存错误

c - 在十六进制数中设置一个位

c - 如何检查来自标准输入的输入是否为空或换行符

c - 当我将 0 作为 getline 的第二个参数传递时会发生什么?

c - "x"类型的参数与 x 类型的参数不兼容

c++ - 是否需要在原始指针位置调用delete[]

java - 使用静态变量节省数组空间