cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic Memory Allocation causes hard fault

akoluacik
Senior

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 exception

In 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.

2 REPLIES 2

Are the stack or heap adequate?

The second here uses the stack.

Does the malloc() return a viable address?

Any useful information from the Hard Fault Handler on the register content or the exact code that's fault.

A while(1) is going to tell you next to nothing

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I've tried to check if malloc allocates space correctly, by using:

if(filter != NULL) {
...
}

However, the program couldn't reach this statement.

I am not sure stack or heap are adequate. The min heap size is adjusted as 0x200 while min_stack_size is 0x400. I've tried to change it but it didn't work. This values are taken in STM32F746NGHx_FLASH.ld file, since I am using BSP project released by ST.