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.

7
  • 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? Commented Aug 2, 2022 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? Commented Aug 3, 2022 at 7: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(). Commented Aug 4, 2022 at 6:41
  • ... we have only your word for it that you are free'ing this buffer appropriately. Commented Aug 4, 2022 at 6: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). Commented Aug 4, 2022 at 6:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.