The following C code is available:

#include <stdio.h> #include <stdlib.h> #include <math.h> #define Te 52.0 #define Ts 2196.0 #define Tc 2196.0 #define Traw_max 246140.0 #define RL 7 const int W[] = {16, 32, 64, 128, 256, 512, 1024}; double mod = 0.0; double lambda(int N, double Traw); int round_p(double x); int main() { FILE *fp; int i; fp = fopen("D:\\data_a.txt", "w"); for(i = 1; i <= 100; i++) {printf("%d\n", i); fprintf(fp, "%d %.9f\n", i, lambda(1, Traw_max / i));} fclose(fp); return 0; } double lambda(int N, double Traw) { if(Traw < Ts) return 0.0; if(N <= 0) return 0.0; printf("mark0\n"); int t = 0, i, j; int max_t = round_p(Traw / Te) + 1; printf("%d\n", max_t); double *Pold[W[0]], *Pnew[W[0]], lambda = 0.0; printf("mark0.5\n"); for(i = 0; i < W[0]; i++) { Pold[i] = calloc(max_t, sizeof(double)); Pnew[i] = calloc(max_t, sizeof(double)); } printf("mark0.7\n"); for(i = 0; i < W[0]; i++) {printf("%d\n", i); Pold[i][0] = 1.0 / W[0]; printf("%e\n", Pold[i][0]);} printf("mark1\n"); while(t < max_t) { for(i = 1; i < W[0]; i++) for(j = 0; j < max_t; j++) Pnew[i - 1][j] = Pold[i][j]; for(j = 0; j < max_t; j++) for(i = 0; i < W[0]; i++) Pnew[i][j + 1] = Pold[0][j] * (1.0 / W[0]); for(i = 0; i < W[0]; i++) for(j = 0; j < max_t; j++) Pold[i][j] = Pnew[i][j]; for(i = 0; i < W[0]; i++) for(j = 0; j < max_t; j++) if((t + 1 - j >= 0) && (j * Ts + (t + 1 - j) * Te > Traw - Ts) && (j * Ts + (t + 1 - j) * Te <= Traw)) { lambda += j * Pold[i][j]; Pold[i][j] = 0.0; } t++; } printf("mark2\n"); for(i = 0; i < W[0]; i++) { free(Pold[i]); free(Pnew[i]); } return lambda; } int round_p(double x) { int i; for(i = 0; i <= x; i++); i++; return i; } 

Which is successfully compiled. However, when you try to start the program crashes. In the task manager, everything is fine - the process eats up the standard 25% of processor time, eats little memory. In some places in the code I placed the labels of the console output, and noticed two of the most frequent cases:

  1. The lambda function is called once, reaches the mark2 mark and the program crashes.

  2. The lambda function is called exactly 61 times (the number of the beast, not otherwise), and the program crashes when trying to assign an element of the array Pold[0][0] in the loop. If you first try to assign a value to an array element outside the loop, then a crash also occurs.

What's the matter? What should be done to understand the cause of the error?

  • I think that very few people will be interested in understanding this meaningless code. - Vlad from Moscow
  • You can simply indicate the possible reasons. Memory is freed, it has always been written to the file. What else do I need to check? - hunter
  • I took note of the round_p(double x) function. I haven't laughed like that for a long time And in fact I want to say that I can’t understand why you call calloc and free . Second, it would not hurt to wrap the construction in a try catch and then it will become clear what the matter is - ArchDemon
  • @hunter To indicate that there is no free memory somewhere, you need to examine this code. And this occupation is not for the faint of heart. - Vlad from Moscow
  • @ArchDemon As far as I know, there are no try / catch constructs in C. - Vlad from Moscow

1 answer 1

Your program has undefined behavior, because at least in these cycles

 for(j = 0; j < max_t; j++) for(i = 0; i < W[0]; i++) Pnew[i][j + 1] = Pold[0][j] * (1.0 / W[0]); 

you go beyond the boundary of the array Pnew since when j equal to max_t - 1 you refer to the element with the index max_t in the expression Pnew[i][j + 1] .

But in any case, you need to rewrite the code, as this is just completely unreadable and meaningless, and I think that there are other obvious bugs.