GDB的奇怪行为

use*_*779 6 c++ gdb

我一直在尝试调试一些小东西,并且在尝试这样做时几乎疯了.经过几个小时的解决问题后,我终于得到了一段代码,这是我问题的根源:

#include <iostream>
#include <vector>
#include <stack>

using namespace std;

int main()
{
    std::vector<int> foo = std::vector<int>();

    foo.push_back(0);
    foo.push_back(11);
    foo.push_back(222);
    foo.push_back(3333);

    std::stack<int> bar = std::stack<int>();

    cout << endl << foo.size() << endl << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

有了这个编译,使用:

g++ -std=c++11 -ggdb -O0 -pedantic -Wall -Wextra -Wno-nonnull -fkeep-inline-functions
Run Code Online (Sandbox Code Playgroud)

然后我尝试以下方法:

(gdb) br 18
Breakpoint 1 at 0x40170c: file ./....cpp, line 18.
(gdb) r
Starting program: ...
[New Thread 15620.0x3c3c]

Breakpoint 1, main () at ./....cpp:18
18              cout << endl << foo.size() << endl << endl;
(gdb) p foo.size()
$1 = 4293588256
(gdb) c
Continuing.

4

[Inferior 1 (process 15620) exited normally]
(gdb)
Run Code Online (Sandbox Code Playgroud)

可悲的是,4现在等于4293588256.到底是怎么回事?此外,如果我在创建堆栈之前中断程序,GDB会正确显示大小.

编辑:我在Windows 8.1,版本的东西是:G ++ 4.8.1; GDB 7.6.1

use*_*779 3

事实证明这确实是 GDB 7.6.1 或 G++ 4.8.1 的问题。将 GDB 更新到 7.8.1,将 G++ 更新到 4.9.2 版本解决了该问题。