在阵列上使用自由函数(C)时,我的程序崩溃了

时间:2016-08-26 03:32:53

标签: c arrays malloc free jagged-arrays

如果我没有释放我在dinamically分配的内存,我的程序可以工作,但当我尝试释放它时,它会崩溃。我在Dev-C ++上运行我的代码并且错误消息没有帮助:问题导致程序停止工作。 问题出现在代码的末尾。如果我采用释放部分,该程序工作正常。以下是输入示例:

5
2.3
1.4 8.2
3.1 2.0 7.7
5.3 6.1 4.4 1.2
5.5 6.1 3.0 2.3 4.9 

代码:

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

int main(void) {
    int ordem;
    scanf("%d", &ordem);
    double **vetor; //apontador para apontador (jagged array)
    double soma = 0;
    int elementos;
    elementos = (ordem*ordem + ordem) / 2;
    vetor = malloc(ordem * sizeof(double*));   //o primeiro endereço do vetor é seu local de memória, que define quantos pointers ele terá (ordem)
    int n, m;
    for (n = 0; n < ordem; n++) {
        *(vetor + n) = malloc(n * sizeof(double));
    }
    for (n = 0; n < ordem; n++) {
        for (m = 0; m <= n; m++) {
            scanf("%lf", *(vetor + n) + m);
            soma = soma + *(*(vetor + n) + m);
        }
    }
    double media = soma / (double) elementos;
    double aux = 0;
    for (n = 0; n < ordem; n++) {
        for (m = 0; m <= n; m++) {
            aux = aux + pow((media - vetor[n][m]), 2);
        }
    }
    double desvio = sqrt(aux / ((double)elementos));
    for (n = 0; n < ordem; n++) {
        for (m = 0; m <= n; m++) {
            printf("%.12lf ", (vetor[n][m] - media) / desvio);
        }
        printf("\n");
    }
    printf("\n%.12lf %.12lf \n", media, desvio);

    for (n = 0; n < ordem; n++) {
        free(*(vetor + n));
    }
    free(vetor);

    return 0;
}

这就是它。我不知道如何解决这个问题。我不认为dealloc具有相同的效果。我更喜欢坚持自由。我发现的新信息:对于样本输入,我可以释放((vetor + 0)),((vetor + 1))和(*(vetor + 2)),但不是3和4这意味着矢量在这两个方面存在问题。

2 个答案:

答案 0 :(得分:3)

改变这个:

for (n = 0; n < ordem; n++) {
    *(vetor + n) = malloc(n * sizeof(double));
}

到此:

for (n = 0; n < ordem; n++) {
    *(vetor + n) = malloc((n + 1) * sizeof(double));
}

原因是n在第一次迭代中是零,所以你要求malloc()为你分配零字节..

顺便说一句,这就是我通常用来做出这种错误的方法:2d-dynamic-array-c

输出:

C02QT2UBFVH6-lm:~ gsamaras$ gcc -Wall main.c 
C02QT2UBFVH6-lm:~ gsamaras$ ./a.out
5
2.3
1.4 8.2
3.1 2.0 7.7
5.3 6.1 4.4 1.2
5.5 6.1 3.0 2.3 4.9
-0.892202112506 
-1.307537578672 1.830552610141 
-0.523015031469 -1.030647267895 1.599810684493 
0.492249441383 0.861436522419 0.076913975216 -1.399834348932 
0.584546211642 0.861436522419 -0.569163416599 -0.892202112506 0.307655900864 

4.233333333333 2.166923061753
C02QT2UBFVH6-lm:~ gsamaras$

答案 1 :(得分:3)

您已经收到了答案,但是如果您对代码有信心,还有其他几个领域需要验证您的输入和分配正在使用实际值而不是写入硬件内存的未知部分。始终始终,验证用户输入和内存分配。只需几行代码就可以提供帮助,例如:

int ordem;
if (scanf ("%d", &ordem) != 1) {
    fprintf (stderr, "error: invalid input (ordem)\n");
    return 1;
}
double **vetor,
       soma = 0;
int elementos, n, m;

elementos = (ordem * ordem + ordem) / 2; 
if (!(vetor = malloc (ordem * sizeof *vetor))) {
    fprintf (stderr, "error: virtual memory exhausted.\n");
    return 1;
}

for (n = 0; n < ordem; n++)
    if (!(*(vetor + n) = malloc ((n + 1) * sizeof **vetor))) {
        fprintf (stderr, "error: virtual memory exhausted.\n");
        return 1;
    }

for (n = 0; n < ordem; n++) {
    for (m = 0; m <= n; m++) {
        if (scanf ("%lf", *(vetor + n) + m) != 1)  {
            fprintf (stderr, "error: invalid input (vetor + %d)\n", n);
            return 1;
        }
        soma = soma + *(*(vetor + n) + m);
    }
}

祝你好运。