Skip to main content
AHnat.1
Visitor II
April 7, 2022
Question

Why does this simple for loop cause run time faults?

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;

}

This topic has been closed for replies.

4 replies

gbm
Super User
April 7, 2022

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.
April 8, 2022

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.

Tesla DeLorean
Guru
April 8, 2022

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal
April 9, 2022

Replace double with float as debug trial.