cancel
Showing results for 
Search instead for 
Did you mean: 

variable i not initialized in for(int i = 0; i < 5; ++i) block in debug mode

XXu.3
Associate II

As figure shows:

0693W00000D1y89QAB.png 

I am using the STM32CUBEIDE V1.7, STM32F407 discovery board. Any help is appreciated.

Thanks,

Xiankun

1 ACCEPTED SOLUTION

Accepted Solutions
XXu.3
Associate II

Hi Tesla and TDK,

Thanks very much for your kind help.

After debugging, I found out the reason is that I manually changed the SP register's value before entering this routine.

I didn't exactly understand why this will cause the weird behavior. As I thought the local variable "i" is in local stack, so the change of SP before calling this routine will have no influence on the behavior of this routine. But obviously carelessly change SP is dangerous.

Thanks

Xiankun

View solution in original post

3 REPLIES 3

Are you using the optimizer? Might want to try with it disabled for the function.

Try looking at the code generated by the compiler, probably holding variable in a register

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

It's probably just not showing exactly what's going on. Note that tid can't be correct either, since it should be < 5.

Defining variables as volatile can stop optimization.

If you feel a post has answered your question, please click "Accept as Solution".
XXu.3
Associate II

Hi Tesla and TDK,

Thanks very much for your kind help.

After debugging, I found out the reason is that I manually changed the SP register's value before entering this routine.

I didn't exactly understand why this will cause the weird behavior. As I thought the local variable "i" is in local stack, so the change of SP before calling this routine will have no influence on the behavior of this routine. But obviously carelessly change SP is dangerous.

Thanks

Xiankun