Is this a gdb error?

1 #include "string" 2 using namespace std; 3 4 bool m_bInited = true; 5 int m_imaxsize = 100; 6 7 int test() 8 { 9 if (!m_bInited) 10 { 11 return -1; 12 } 13 14 std::string gbkInput = ""; 15 std::string utf8Input = ""; 16 if (gbkInput.size() > m_imaxsize) 17 { 18 return 1; 19 } 20 return 0; 21 } 22 23 int main() 24 { 25 test(); 26 return 0; 27 } 

when using the gdb step from line 16, the debugging sequence:

line 16 β†’ line 20 β†’ line 18 β†’ line 21.

  (gdb) b 16 (gdb) r Breakpoint 1, test () at main.cpp:16 16 if (gbkInput.size() > m_imaxsize) (gdb) n 20 return 0; (gdb) n 18 return 1; (gdb) n 21 } 

compile: g ++ -g main.cpp -o test

why does gdb display line 18? and the return value of test () is 0.

My gcc version is 4.1.2. GNU gdb Fedora (6.8-37.el5) or GNU gdb (GDB) Red Hat Enterprise Linux (7.0.1-37.el5). The gdb version also has this problem.

BTW: if the translation line is 14, line 15 (these two lines are var) to line 9, this will be fine. gdb does not display line 18! it seemed like the var line was causing this error.

Can everyone help me? Thanks!

+4
source share
1 answer

This behavior is a β€œfunction” of older versions of gcc / gdb, and it was previously reported about this: unexpected behavior of gdb: in nested if . Note. This question cannot be marked as a duplicate of this other, since there was no satisfactory solution.

The statement is not executed. He just looks.

Adding
It is easy to verify that the statement is not executed. Add function

 int one() { return 1; } 

Then replace return 1; on return one(); gdb will print return one(); but it does not call the one() function. There seems to be a problem in older versions of gdb with displaying the closing brace in an if statement.

Remember well: this only happens with older versions of gdb, and this is apparently a display problem, not a program malfunctioning.

+4
source

All Articles