1

I am new to C++. When I debugged in Clion, I found that the execution order using Step over (F8) doesn't match the real code's order. So far, I think the most possible reason is compiler optimization. I have no impression that I've enabled this and I am using LLDB to debug.

It seems related to this answer: Why don't my breakpoints run in order? GCC and C

Here is my code:

int depthSum(vector<NestedInteger> &nestedList) {
        deque<pair<int, NestedInteger>> q;
        int level = 1;
        for (NestedInteger n : nestedList) {
            q.push_back({ level, n});
        }

        int res = 0;
        while (!q.empty()) {
            auto pair = q.front();
            q.pop_front();
            if (pair.second.isInteger()) {
                level--;
                res = (level * (pair.second.getInteger()));
                cout << res << endl;
            } else {
                level++;
                for (NestedInteger nestedInteger : pair.second.getList()) {
                    q.push_front({level, nestedInteger});
                }
            }
        }
        return res;
    }

When the breakpoint step at the res = (level * (pair.second.getInteger())); and then press the F8 it jumps back to the level--, and then press F8 jump to the res = (level * (pair.second.getInteger()));, once again press F8 that will jump to the cout << res << endl;

marked order: screenshot of debugger with order of jumps

Is this really because code reordering? By the way, the result of the code logical without error though debugging order not match expectation, but I have not found some open code optimization option in my project.


add profile of CMake enter image description here

I tried to avoid optimization but problem still exists.

enter image description here

---------------------------- updated

This is working, but I have no clue why -O0 is not working. Logically speaking -O0 should be working because this avoids all optimization.

-DCMAKE_C_FLAGS_DEBUG:STRING="-g -O1" -DCMAKE_CXX_FLAGS_DEBUG:STRING="-g -O1"
12
  • 2
    This might be of interest, it's about the C++ as if rule. That said I'm surprised if what you describe is what is really happening. I'd be more inclined to think it's a CLion bug in its interactions with the debugger. If you want to be sure try looking at the generated assembly code. Commented Nov 17, 2022 at 6:56
  • What level of optimizing do you compile with? Generally debugging should be done without optimizing, because at higher optimizing levels the relationship between your lines of code and the instructions executed get a bit wonky (because of reordering, code elimination, "as if" rewrites, etc.). Commented Nov 17, 2022 at 7:53
  • 2
    When code has been optimized there is no simple relationship between lines of code and executed code, so in-source debugging often doesn't follow the source. For instance, some code have been removed entirely, some bits can be interleaved with other bits, and loops may be rearranged. Commented Nov 17, 2022 at 8:10
  • Stuff like this is exactly why Why does clang produce inefficient asm with -O0 (for this simple floating point sum)? - -O0 forces each C statement to compile to a contiguous block of instructions, so single-stepping works, and even GDB j to jump to a new source line in the same function works as if you did that in the C++ abstract machine. Commented Nov 17, 2022 at 9:47
  • 2
    If you turn optimisations off, some (but not all) apparent out-of-order execution should disappear. What remains is mostly related to execution of destructors as variables go out of scope. You will notice that as you step out of block, the current line pointer will pass over lines that contain declarations of variables that have destructors, in reverse order of appearance. Commented Nov 21, 2022 at 5:47

1 Answer 1

0

you're IDE probably is in release mode configuration ... so change it to debug mode

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.