Arduino code crashes when arrays are not static

Viewed 56

I am writing a piece of code in Arduino IDE for the chip ESP32. I have a function in a .c file with the following signature that does the convolution between two arrays, Signal and Kernel and returns the output in convout.

void xconvolution(const float *Signal, const int siglen, const float *Kernel, const int kernlen, float *convout);

Now, if in the usage (i.e., in the .ino file) I define the arrays static float , and their lengths as constants with defines it works fine. So, for example, this code works fine:

#define SIG_LEN 1000
#define KER_LEN 500

void setup {
    static float signal[SIG_LEN] = {1, 2, 3, .....};
    static float kenrel[KER_LEN] = {1,0,0,1, .....};
    static float output[SIG_LEN + KER_LEN + 1];

    xconvolution(signal, SIG_LEN, kenrel, KER_LEN, &output[0]);
}

However, I don't know the size of the arrays and their values before runtime, so if I try

void setup {
    int SIG_LEN = random(1000) + 1; //Generate random nr
    int KER_LEN = random(700) + 1; //Generate random nr
    
    float signal[SIG_LEN];
    float kenrel[KER_LEN];
    float output[SIG_LEN + KER_LEN + 1];
    
    // Add elements to the arrays 
    // .....

    xconvolution(signal, SIG_LEN, kenrel, KER_LEN, &output[0]);
}

I am still able to compile without errors but the code crashes when I upload it to the board with the error:

***ERROR*** A stack overflow in task loopTask has been detected.


Backtrace:0x400835c9:0x3ffd5fd00x40089811:0x3ffd5ff0 0x4008c3d1:0x3ffd6010 0x4008b11b:0x3ffd6090 0x40089920:0x3ffd60c0 0x400898d0:0x000001e0  |<-CORRUPTED

Any idea why or what I could do to avoid it? Please, let me know if you need more details and I'll provide them.

Thank you

0 Answers
Related