0

I am trying to implement a system so that it retrieves sound and extracts the mfcc of it. I'd like to implement my own mfcc function because librosa library wasn't implemented in C and other implementations of mfcc extractions doesn't yield the same outputs as librosa library does. So I wrote a code, however, when I would like create hanning window, program doesn't take a step further and always stays the same statement while debugging. The statement is below:

float *mul = malloc(sizeof(float)*fftsize);

The whole code is as follows:

float* hanning(int fftsize){

     float *mul = malloc(sizeof(float)*fftsize);

     for (int i = 0; i<fftsize; i++){

         mul[i] = 0.5 * (1 - cos(2*PI*i/(fftsize-1)));

     }

     return mul;
}

I put an LCD code to all error handler functions in stm32f7xx_it.c file to determine which fault I'm facing, and I see that it is hard_fault.

So what's the problem? I hope the issue is explained clearly. Due to the privacy, I couldn't put here whole code. Sorry for that. Thx in advance for your response.

Edit: I am chaning malloc to normal array with a variable length array. But still it takes me to HardFault_Handler function. SCB->SHCSR returns sometimes 65535 and sometimes 1.

  • 1
    Is malloc getting memory from the heap? Where is the heap located? Is the heap big enough? Did you remember to free the buffer when you're done with it? Why not use a statically allocated buffer instead of dynamically allocating it? – kkrambo Aug 02 '22 at 20:53
  • 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. There is no memory allocation before this statement so I don't think it is because I forgot to free, but I inkoved free function when I'm done with that. I don't use array because I need variable size array and I though I can do it with dynamic memory. And lastly, as far as I know, malloc already allocates memory in the heap region, is it wrong? Do I need to something to do it? – akoluaciklinux Aug 03 '22 at 07:46
  • 1
    On Cortex-M "hardfault" is a catch-all for when more selective faults are not enabled (which they are not by default). Inspect the SHCSR register to determine the fault type more specifically. Most likely you have insufficient heap space. 0x200 looks is a default minimal allocation to allow the runtime environment to be linked, but not intended for use. You say you modified it, but not to what, and you have not specified the max value of `fftsize`. It is I'll advised to hide an allocation in a function and free elsewhere. Better to pass a buffer _into_ `hanning()`. – Clifford Aug 04 '22 at 06:41
  • ... we have only your word for it that you are free'ing this buffer appropriately. – Clifford Aug 04 '22 at 06:43
  • 1
    Note also that being of variable `fftsize` on its own is no justification for dynamic memory allocation if the maximum `fftsize` is known. The only advantage of using the heap is that that space can be reused for other purposes at runtime. If you are not doing that and this is the only allocation from heap, you may as well simply allocate an `FFTSIZE_MAX` buffer statically since you would in any event need a heap of more than `FFTSIZE_MAX * sizeof(float)` in any case. `malloc` is not a _magic memory tree_, the heap itself is statically allocated (in this case). – Clifford Aug 04 '22 at 06:55
  • @Clifford, your last statement is true. Actually there are two parts that I am calling hanning function. In the first part, the fftsize is known and I am creating a buffer as you mentioned. But for the second point, the length of the buffer is variable, actually I can create a variable length array, and I tried it, but this time it works for two iteration, and again goes into hard fault handler. I used `SCB->SHCSR` to check the fault type, and it gives 65535. I find it a little bit strange, I cannot find any information about register, so I couldn't make any comment about it. – akoluaciklinux Aug 04 '22 at 11:33
  • @akoluaciklinux : you have missed my point, your question needs more information and context. Relevant information should be in the he questions, not comments. – Clifford Aug 04 '22 at 20:11

0 Answers0