C strange array allocation behavior

I use xcode with c and I have a strange problem with the following code. When the algorithm reaches the y[k] = 0.0 row in my conv function then the x array filled by 0-s, I must understand why, I already correct this error in my own way. The problem is not appear when I comment the second row and uncomment the third row in main function(see below). I know that I reallocate the array in generateSquareSignal, but this is not a reason for this bug I think. int length = 100; double *output = (double*) malloc(10 * length * sizeof(double)); //double *output; output = generateSquareSignal(length); double *input1 = (double*) malloc(length * sizeof(double)); double *input2 = (double*) malloc(length * sizeof(double)); for (int i = 0; i < length; i++) { input2[i] = output[i]; input1[i] = output[i]; //printf("-%d=%lf\n",i ,input1[i]); } conv(input1, length, input2, length, output, 2 * length ); double* generateSquareSignal(int length) { printf("double* generateSquareSignal(int length)\n"); double *signal = (double*) malloc(length * sizeof(double)); int length_period = length / kPeriodSignal; for(int i=0; i < length; i++) { if (i % (length_period) < (length_period / 2)) { signal[i] = 1.0; } else { signal[i] = -1.0; } //printf("%d. - %lf\n", i, signal[i]); } return signal; } void conv( double *x, int N_signal, double *h, int N_kernel, double *y, int N_output) { int k; for(k = 0; k < N_signal + N_kernel; k++) { y[k] = 0.0; } for (int i = 0; i < N_signal; i++) { printf("%lf-%lf\n", x[i], y[i]); } for(int i = 0; i < N_signal; i++) { for(int j = 0; j < N_kernel; j++) { double xx = x[i]; double hh = h[j]; double yy = y[i + j]; y[i + j] += x[i] * h[j]; //printf("=%lf\n", y[i + j]); } } }
don't cast the return of malloc: stackoverflow.com/questions/605845/…

以上就是C strange array allocation behavior的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » CSS3 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏