简体   繁体   中英

VS2017 C++ debugger skipping lines of code

I did a quick search and found nothing similar to this question. Maybe I don't know how to search for this one, sorry.

I recently moved to VS2017 (community version) from VS2015. I'm involved in a project where I develop in C++. Sometimes I need to debug, as usual.

I noticed the debugger on VS2017 doesn't work as it did on 2015 version. I don't know if it's a configuration problem.

The problem is that Step over, Step Into, Step Out don't work the expected way (execute a code line and move to the next one/etc). I uploaded here some screenshots of the debugger behaviour: https://imgur.com/a/FZSMFSJ

Just pressing F10 (Step Over), the debugger moves that way (line 222->239->242->244->248->244->248->254), skipping code lines in between, even moving forwards and backwards.

As it is seen in the other images, the breakpoint works alright, and there are 2 types of "debug line indicators", with different message.

How can I set the VS options to get the wanted debug?

When optimizations are enabled, the compiler is free to optimize away certain pieces of code. For instance, performing a redundant assignment. However, even with all optimizations disabled, the C++ standard mandates certain optimizations (eg some forms of RVO object initialization).

From the code you show, I'd say it's likely that the compiler is reordering the initialization of the (many) local variables and also perhaps aliasing them from their sources instead of copying them. This would conceivably be done to reduce memory usage inside the function and is safe so long as you don't take the address of the local variable nor make a reference to it.

Additionally, though the compiler guarantees that your code will function as if it were executed sequentially (as you wrote it), most compilers (with optimizations enabled) will reorder things that it can guarantee will not break your code for efficiency reasons. This is typically done to minimize memory accesses, enable vectorization, or for hardware-specific instruction pipelining reasons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM