MATLAB: Stack overflow using the generated C++ code

matlab coder

I generated the code using Matlab Coder, and integrated into MSVC of another project. During run time, it gives stack overflow error. I tried to use memory optimization feature (Enable variable-sizing, and reduce stake usage max to 100,000). The simulation goes a little further but still give stack overflow in another function call. Just need to know how to address it.
Really appreciate it!

Best Answer

Finally I got the solution. The issue is not static variable. The issue is that the function variable size is too big.
Here is the solution: when generate the code. in the memory configuration, by default Stake usage max is 200,000. But for MSVC, the default is 100,000. So I reduce it to 50,000 (consider other functions use another 50,000). Also I enable "Variable-sizing". And change the dynamic memory allocation threshold to 4000 (by default,it is 65,536). I use 4000 because it has many variables with double[600] type (size of 4800), thus I enforce the tool to use static variable instead of regular variable. Once it is done. No stack overflow issue anymore.
Thanks for the help from Denis.