arrays - How to use malloc in a c function? -


i want make c function fir filter, has 2 input arrays , 1 output array. both input arrays constant numbers, want use them computation of output of filter,and after computation delete them , store output array of function code not work

#include <stdlib.h> float   * filter(float *patientsignal,float *filtercoef, int lengthofpatient , int lengthoffilter ){     static float firout[8000];     int i,j;     float temp=0;     float* signal;     float* coef;     signal = malloc(lengthofpatient *sizeof(float));     coef = malloc(lengthoffilter*sizeof(float));     }     (j = 0; j <= lengthofpatient; j++){         temp = signal[j] * coef[0];         (i = 1; <= lengthoffilter; i++){             if ((j - i) >= 0){                 temp += signal[j - i] * coef[i];             }             firout[j] = temp;         }     }     free(signal);     free(coef);     free(patientsignal);     return firout; } 

there several problems in code,

  1. unnecessary } after line coef = malloc(lengthoffilter*sizeof(float));.

  2. for (j = 0; j <= lengthofpatient; j++). loop once more required. same loop. pmg mentioned in comment.

  3. temp += signal[j - i] * coef[i]; not give desired outcome, not initialized both signal or coef.

  4. whats purpose of float *patientsignal,float *filtercoef in function parameter?

from wild guess, think need 2 line initialize signal and/or coef.

memccpy(signal, patientsignal, lengthofpatient); memccpy(coef, filtercoef, lengthoffilter); 

don't free patientsignal in local function. let done function caller.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -