breaking at for-loop test line

Allen Hopkins allenh@eecs.berkeley.edu
Thu Jul 29 05:10:00 GMT 2004


Is there a way to break at the test statement of a for-loop 
on every iteration, the same way a while-loop works?  Here's
what I mean:

      1	#include <iostream.h>
      2	
      3	int main(int argc, char* argv[])
      4	{
      5	    int i = 0;
      6	
      7	    for (i = 0; i < 3; i++) {
      8	        cout << i << endl;
      9	    }
     10	
     11	    while (i < 6) {
     12	        cout << i++ << endl;
     13	    }
     14	
     15	    exit(0);
     16	}

If I set a breakpoint at line 7, and another at line 11,
and "run" and "continue" until exit, it will only break once
on the for-loop, but it will break on each iteration of the
while-loop.  How does this make sense?  Is there any way to
get a breakpoint at the top of the for-loop to act like the
while-loop?

Here's the compile command:
> g++ -ggdb -Wno-deprecated -o s s.cpp

There's no optimization going on that I'm not aware of,
is there?

Here's sample gdb output:
(gdb) break 7
Breakpoint 1 at 0x10b04: file s.cpp, line 7.
(gdb) break 11
Breakpoint 2 at 0x10b5c: file s.cpp, line 11.
(gdb) run
Starting program: 
/export/home/allenh/projects/metropolis/testing/foodir/s

Breakpoint 1, main (argc=1, argv=0xffbeecac) at s.cpp:7
7	    for (i = 0; i < 3; i++) {
(gdb) c
Continuing.
0
1
2

Breakpoint 2, main (argc=1, argv=0xffbeecac) at s.cpp:11
11	    while (i < 6) {
(gdb) c
Continuing.
3

Breakpoint 2, main (argc=1, argv=0xffbeecac) at s.cpp:11
11	    while (i < 6) {
(gdb) c
Continuing.
4

Breakpoint 2, main (argc=1, argv=0xffbeecac) at s.cpp:11
11	    while (i < 6) {
(gdb) c
Continuing.
5

Breakpoint 2, main (argc=1, argv=0xffbeecac) at s.cpp:11
11	    while (i < 6) {
(gdb) c
Continuing.

Program exited normally.
(gdb)

Any advice appreciated.

Oh, yeah...

	Sun Solaris 2.8
	gcc 3.2.2
	gdb 6.0

Thanks.
-Allen Hopkins
UC Berkeley



More information about the Gdb mailing list