cancel
Showing results for 
Search instead for 
Did you mean: 

Why does this simple for loop cause run time faults?

AHnat.1
Associate

I have made a for loop that produces the data of a sine wave and stores the values in an array. The loop is set to execute in the main(). However this seems to break whatever board i use, i have tested on L47RG and the H745i. Any help would be appreciated.

Below is the for loop that is executed in the main.c, main().

N is a defined constant of 100, and TMAX is a defined constant of 1.0

double t[N];

 double y[N];

/*data to be fitted*/

for (int i = 0; i < N; i++)

{

double ti = i * TMAX / 100;

double yi = sin(5 * ti + 0.6);

t[i] = ti;

y[i] = yi;

}

4 REPLIES 4
gbm
Lead III

If t and y are declared inside main() or any other functin, they are created on the stack, occupying 1600 bytes. That's possibly dangerous - don't use stack fro big data structures. Double type is not handled by hardware on L476 - that may be another problem.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
Pavel A.
Evangelist III

Which runtime fault? CubeIDE and other decent IDEs have a fault analyzer to help you understand what happened.

1600 bytes on stack by itself is not dangerous. It should be something else.

Do some proper debugging...

Understand the mechanics of the failure.

Have a proper Hard Fault Handler so you can understand if and why it ends up there.

Use the debugger, look at the registers, inspect the stack, see where it's hung up.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal

Replace double with float as debug trial.