Why does gdb show two different returns?

Posted by YuppieNetworking on Stack Overflow See other posts from Stack Overflow or by YuppieNetworking
Published on 2010-05-10T12:03:34Z Indexed on 2010/05/10 12:14 UTC
Read the original article Hit count: 611

Filed under:
|
|

Hello all,

Last week I was a debugging a code and a weird situation came up: gdb passes through two different return clauses. I made a simple example that illustrates the situation:

#include <iostream>

using namespace std;

int test() {

    string a = "asd";
    string b = "asd";

    while (true) {
        if (a == b) {
            return 0;
        }
    }
    return -1;
}

int main() {
    int result = test();
    cout << "result: " << result << endl;
}

When debugging the code I got:

(gdb) b main
Breakpoint 1 at 0x1d4c: file example.cpp, line 19.
(gdb) r
Starting program: /Users/yuppienet/temp/a.out 
Reading symbols for shared libraries +++. done

Breakpoint 1, main () at example.cpp:19
19          int result = test();
(gdb) s
test () at example.cpp:7
7           string a = "asd";
(gdb) n
8           string b = "asd";
(gdb) n
11              if (a == b) {
(gdb) n
12                  return 0;
(gdb) n
15          return -1;
(gdb) n
16      }
(gdb) n
main () at example.cpp:20
20          cout << "result: " << result << endl;
(gdb) n
result: 0
21      }
(gdb) n
0x00001ab2 in start ()

I noted that even if gdb shows line 15, the return value is 0 (the finish command confirms this as well).

So the question is: why does gdb show line 15: return -1, even if the function is not really returning this value?

Thanks!

© Stack Overflow or respective owner

Related posts about gdb

Related posts about c