title: Why segmentation fault and Program finished with exit code 139?
body: <p>I try to calculate retrospectively functions about possibilities with c language and write the result to a text. The result running my program with code blocks 17.12 is:</p>
  
  <pre><code>"Linking stage skipped (build target has no object files to link" and
  "Nothing to be done (all items are up-to-date)"
  </code></pre>
  
  <p>Also, i try running this code with online compiler(<a href="https://www.onlinegdb.com/online_c_compiler" rel="nofollow noreferrer">https://www.onlinegdb.com/online_c_compiler</a>) and the result was:
  "Segmentation fault" and "Program finished with exit code 139"</p>
  
  <p>I believe that my problem is about memory allocate with 3D and 2D Multidimensional arrays.</p>
  
  <p>Please help me!!</p>
  
  <p>The code is:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  
  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 &lt; 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 &lt; 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 &lt; C; i++)
      {
          m[i] = (double *) malloc(B * sizeof(double));
      }
      for (i = 1; i &lt; 7; ++i)
      {
          for (N = 1; N &lt; 545; ++N)
          {
              for (k = 0; k &lt; 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 &lt; 7; ++i)
      {
          p[i][0][0] = 1;
          for (N = 1; N &lt; 545; ++N)
          {
              for (k = 0; k &lt; N + 1; ++k)
              {
                  if (k == 0)
                  {
                      for (l = 1; l &lt; 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 &lt; 7; ++i)
      {
          for (N = 1; N &lt; 545; ++N)
          {
              for (k = 0; k &lt; 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;
  }
  </code></pre>