如何在 CLion 中设置断点条件

How to set breakpoint conditions in CLion

我在使用 C 应用程序时遇到了一些问题。我正在写 CLion (windows),我有一个来自 1 to 1000for 循环,但在 i = 600 附近的某个时刻,循环内的代码 returns 有问题。

在这一点上,我对为什么会出现这个问题不感兴趣,而是对如何找到它感兴趣,所以我尝试调试应用程序,但不可能命中 F7 600 次。

那么有什么方法可以让我在达到 590 时开始调试?

CLion 允许您设置条件断点。考虑下面的代码,它会在循环 601 上表现出不良行为,因为它 运行 离开数组的末尾。

要捕获它,请在进行赋值的行上设置一个断点。

然后,右击断点,在对话框的条件框中添加如下内容:

i == 599

然后,运行代码,调试器只有在i == 599时才会停在断点处,很神奇!

#include <stdio.h>

int main()
{
    char array[600];

    for (int i = 0; i < 1000; i++) {
        // code that does something

        array[i] = 0xff; // set breakpoint here!
        //, then right click and add conditional:  i == 590
    }

    printf("Hello, World!\n");
    return 0;
}