Dynamic Memory Allocation causes hard fault
I am trying to implement a function that extracts mfcc of the retrieved sound. I am using Hanning window. I actually am applying dynamic memory allocation twice. First allocation works successfully. Second allocation however somehow takes me to hard fault handler function.
float number_of_freq_bins = ceilf(hz_BW/(float)(SAMPLE_RATE/(FFTSIZE>>1)));
if (((int)number_of_freq_bins % 2) == 0) number_of_freq_bins++;
size_t len = (size_t)number_of_freq_bins; // just for having appropriate datatype for allocation
hanning(offset, number_of_freq_bins);
float* filter = malloc(sizeof(float) * len); // yields hard fault exceptionIn line 6, hanning is a function that creates hanning window, and seems as below:
void hanning(int offset, int fftsize){
//float *mul = malloc(sizeof(float)*fftsize);
float mul[fftsize];
for (int i = 0; i<fftsize; i++){
mul[i] = 0.5 * (1 - cos(2*PI*i/(fftsize-1)));
}
// AUDIO_HANN_ADDRESS 0xC0400000
memcpy((uint32_t*)(AUDIO_HANN_ADDRES + offset), mul, fftsize);
}Edit: I changed the statement that contains malloc to normal array.
This time, since the statement is inside a loop, it works fine until the loop is terminated. But when the loop, and hence the function is terminated, it takes me to hard_fault handler function. And SCB->SHCSR yields sometimes 1 and sometimes 65535.