Hi When I use optimization level 2 or 3, the debbuger crash and the Uvision halt. I'm using uVision 4.21 and ULINK Pro with STM32F103ZC (the same problem also with the ST-LINK V2). Thanks
The compiler have no reason to notice if code is too large. The compiler do not care.
The linker would care, except that the linker doesn't know how much memory the device has. The linker just link based on the information entered in the project settings. Tellit a 8kB chip has 16kB flash, and it will link happily twice as much code as the device can handle.
I'm not so fond of your "I expect". It is a wording that is very closely associated with "I assume". It tends to lead to problems, when the assumptions/expectations failed to match the reality. The majority of all bugs that hurt us everyday are caused by "I assume" or "I expect".
So, have you verified that the code gets downloaded 100% correct?
Have you verified that you can start the debugger from the start of the code? Don't do the run to "main" while trying to track down your problem.