Skip to main content
XXu.3
Associate II
August 10, 2021
Solved

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

As figure shows:

0693W00000D1y89QAB.png 

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

Thanks,

Xiankun

This topic has been closed for replies.
XXu.3
XXu.3Best answer
Associate II
August 12, 2021

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

3 replies

Tesla DeLorean
Guru
August 10, 2021

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
TDK
August 10, 2021

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
XXu.3AuthorBest answer
Associate II
August 12, 2021

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