デバッグするときに「特定のループ回数で止めて、値を確認したい」ってのはよくあることです。
if を使うとそれができます。
#include <stdio.h>
int main(int argc, char *argv[])
{
for (int i=0; i<10; i++)
{
printf("i=[%02d]\n", i);
}
return 0;
}
というソースを書いてデバッグします。
> gdb a.out
GNU gdb 6.1.1 [FreeBSD]
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "amd64-marcel-freebsd"...
(gdb) b main
Breakpoint 1 at 0x4007b6: file breakif.cpp, line 5.
(gdb) r
Starting program: /usr/home/kitayama/lang/cpp/sample/a.out
Breakpoint 1, main (argc=1, argv=0x7fffffffe480) at breakif.cpp:5
5 for (int i=0; i<10; i++)
(gdb) l
1 #include <stdio.h>
2
3 int main(int argc, char *argv[])
4 {
5 for (int i=0; i<10; i++)
6 {
7 printf("i=[%02d]\n", i);
8 }
9
10 return 0;
(gdb) break 7 if i==5
Breakpoint 2 at 0x4007d1: file breakif.cpp, line 7.
(gdb) c
Continuing.
i=[00]
i=[01]
i=[02]
i=[03]
i=[04]
Breakpoint 2, main (argc=1, argv=0x7fffffffe480) at breakif.cpp:7
7 printf("i=[%02d]\n", i);