Extending Python Project with C++ Code

1 minute read

Published:

Recently, our project needs to test the ported (from Python code) c++ library. The best way to test might just to directly call the c++ function using the original python framework. Therefore there is a learning curve for me to learn how to use python to call c++ functions.

Some valuable feedbacks from collaborator

  1. Avoid using pointers at all cost, e.g., try to avoid double* npFFTFreq(int nfft, double d). It’s now the time okay to use vector<double> as the return type, since the compiler is clever enough to use a “move” operation to move your local vector variable defined in the function out of it without doing the “copy” operation that compiles do in the old days.
  2. The issue of double* freq_grid = npFFTFreq (nfft, 1.0/frame_rate); is that each time you run it, a new block of memory is created by new in your function and it’s never cleaned up until the destroy of class personRPPG or until the end of the program. This is a memory leakage issue, unless you explicitly delete freq_grid; after using, which is too much to worry about. Of course, you can allocate the memory outside npFFTFreq() but again it’s the old style.