Skip to main content
akoluacik
Associate III
August 3, 2022
Question

Dynamic Memory Allocation causes hard fault

  • August 3, 2022
  • 1 reply
  • 1434 views

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.

This topic has been closed for replies.

1 reply

Tesla DeLorean
Guru
August 3, 2022

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 VenmoUp vote any posts that you find helpful, it shows what's working..
akoluacik
akoluacikAuthor
Associate III
August 4, 2022

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.