unusual behaviour with gdb

When i use the step command in gdb instead of going forwared it jumps to different lines in gdb while debugging c++ code. i'm using cmake to build the code.
You probably use the release version for your debugging. Make sure that you use the debug version.
To add to coder777's words, if you're building with optimisations turned on, then various lines of your source code will be re-ordered, maybe eliminated entirely, replaced with something simpler, and a host of other things that optimisers do to make your code faster. So when you step to the next instruction, maybe that instruction actually corresponds to a line that was earlier in your source code. Or five lines later.

Typically, a debug version will have those optimisation turned off (or at least, turned down a lot) so that your source code more closely matches the generated executable.

But wait! You are now thinking "But doesn't that mean that there could be a bug in my release version, with all the optimisations turned on, that doesn't exist in the debug version?" Yes, it does mean that. "So how can I debug a bug that vanishes when I try to look at it?" How indeed.... :)
Last edited on
So the upshot is to compile with -O0 -g (no optimizations, include debug information)
Topic archived. No new replies allowed.