c++memory-managementembeddedkissfft

How to deallocate memory allocated by Kiss FFT?


I'm working on a C++ project using a FRDM-KL25Z board to measure vibration. My code is working but I need it running on a loop. I'm having problems with memory, I don't have enough space to store the values twice. I used free() commands but even that I'm nothing get all my memory back. Someone knows how to clean all memory used by kiss-fft? It doesn't have a free function, or at list the one that it has doesn't work properly.

I have enough memory to run it just once and show the results. I was trying to do a loop with this function but even using free command I'm nothing getting my initial memory back.

{

void TestFftReal(const char* title, const kiss_fft_scalar in[L], kiss_fft_cpx out[L / 2 + 1])
  kiss_fftr_cfg cfg;
  cfg = kiss_fftr_alloc(L, 0/*is_inverse_fft*/, NULL, NULL);

  if (cfg != NULL) {
      size_t i;
      kiss_fftr(cfg, in, out);
      free(cfg);
  /// Do stuff ///
  } else {
     printf("Not enough memory.\n");
      exit(-1);
  } 
}

Solution

  • The file kiss_fft.h lists a switch called KISS_FFT_USE_ALLOCA. If you define this macro while compiling, then the needed memory is allocated on the stack instead of using malloc. It is automatically freed when the function ends.

    You will have to write your loop body to contain a function to allocate the space and run the fft, so that it returns (and frees the space) before the next loop iteration.