为什么分段错误和程序以退出代码139完成?

时间:2018-07-04 05:23:10

标签: c multidimensional-array segmentation-fault

我尝试使用C语言回顾性地计算关于可能性的函数,并将结果写入文本。使用代码块17.12运行我的程序的结果是:

"Linking stage skipped (build target has no object files to link" and
"Nothing to be done (all items are up-to-date)"

此外,我尝试使用在线编译器(https://www.onlinegdb.com/online_c_compiler)运行此代码,结果是: “分段错误”和“程序以退出代码139完成”

我相信我的问题是关于3D和2D多维数组的内存分配。

请帮助我!

代码是:

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

int main()
{
    double z[7] = { 0, 19.08, 16.86, 11.72, 14.49, 10.87, 11.63 };
    int N, k, i, j, l, fptr;
    int A = 6, B = 544, C = 7;

    double *** p = (double ***) malloc(A * sizeof(double**));
    double *** X = (double ***) malloc(A * sizeof(double**));
    double *** R = (double ***) malloc(A * sizeof(double**));
    double ** sum = (double **) malloc(A * sizeof(double*));
    double ** sum2 = (double **) malloc(A * sizeof(double*));
    double ** m = (double **) malloc(C * sizeof(double*));

    for (i = 0; i < A; i++)
    {
        p[i] = (double **) malloc(B * sizeof(double *));
        X[i] = (double **) malloc(B * sizeof(double *));
        R[i] = (double **) malloc(B * sizeof(double *));

        for (j = 0; j < B; j++)
        {
            p[i][j] = (double *) malloc(B * sizeof(double));
            X[i][j] = (double *) malloc(B * sizeof(double));
            R[i][j] = (double *) malloc(B * sizeof(double));
            sum[i] = (double *) malloc(B * sizeof(double));
            sum2[i] = (double *) malloc(B * sizeof(double));
        }
    }

    for (i = 0; i < C; i++)
    {
        m[i] = (double *) malloc(B * sizeof(double));
    }
    for (i = 1; i < 7; ++i)
    {
        for (N = 1; N < 545; ++N)
        {
            for (k = 0; k < N + 1; ++k)
            {
                m[C][B] = 0;
                p[A][B][B] = 0;
                X[A][B][B] = 0;
                R[A][B][B] = 0;
                sum2[A][B] = 0;
                sum[A][B] = 0;
            }
        }
    }
    for (i = 1; i < 7; ++i)
    {
        p[i][0][0] = 1;
        for (N = 1; N < 545; ++N)
        {
            for (k = 0; k < N + 1; ++k)
            {
                if (k == 0)
                {
                    for (l = 1; l < N + 1; ++l)
                    {
                        p[i][l][N] = pow(0.5 * N / 544, l);
                        sum[i][N] += p[i][l][N];
                    }
                    p[i][k][N] = 1 - sum[i][N];
                }
                if (k != 0)
                {
                    p[i][k - 1][N - 1] = pow(0.5 * (N) / 544, k - 1);
                    m[i][k] = k * z[i];
                    X[i][k][N] = p[i][k][N] * m[i][k] / p[i][k - 1][N - 1];
                    // m[i][k] = X[i][N] * p[i][k-1][N-1] / p[i][k][N];
                    printf("X = %.2lf", X[i][k][N]);
                    sum2[i][N] += k * p[i][k - 1][N - 1] / m[i][k];
                }
                if (k == N)
                {
                    R[i][k][N] = m[i][k] * sum2[i][N];
                    printf("R = %.2lf", R[i][k][N]);
                }
            }
        }
    }
    for (i = 1; i < 7; ++i)
    {
        for (N = 1; N < 545; ++N)
        {
            for (k = 0; k < N + 1; ++k)
            {
                FILE *fptr;
                fptr = fopen("C:\\program.txt", "w");

                if (fptr == NULL)
                {
                    printf("Error!");
                    exit(1);
                }
                fprintf(fptr, "%.21f", X[i][k][N]);
                fprintf(fptr, "%.21f", R[i][k][N]);

                fclose(fptr);
            }
        }
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您正在尝试访问尚未分配给您的内存。

在第17行:分配给m的空间为C*sizeof(double*)

double ** m = (double **)malloc(C*sizeof(double*));

但是稍后您将尝试访问第46行:

m[C][B] = 0;

这是一个问题,因为您只能访问m[C-1]