GDB 的奇怪行为
Weird behaviour of 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;
}
编译后,使用:
g++ -std=c++11 -ggdb -O0 -pedantic -Wall -Wextra -Wno-nonnull -fkeep-inline-functions
然后我尝试以下操作:
(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()
= 4293588256
(gdb) c
Continuing.
4
[Inferior 1 (process 15620) exited normally]
(gdb)
显然,4 现在等于 4293588256。这到底是怎么回事?
另外,如果我在创建堆栈之前中断程序,GDB 会正确显示大小。
编辑:
我在 windows 8.1,东西的版本是:G++ 4.8.1; GDB 7.6.1
事实证明这确实是 GDB 7.6.1 或 G++ 4.8.1 的问题。
将 GDB 更新到 7.8.1 并将 G++ 更新到版本 4.9.2 解决了这个问题。
我一直在尝试调试一些小东西,并且在尝试这样做时差点发疯。经过几个小时的解决问题,我终于有了一段代码,它是我问题的根源:
#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;
}
编译后,使用:
g++ -std=c++11 -ggdb -O0 -pedantic -Wall -Wextra -Wno-nonnull -fkeep-inline-functions
然后我尝试以下操作:
(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()
= 4293588256
(gdb) c
Continuing.
4
[Inferior 1 (process 15620) exited normally]
(gdb)
显然,4 现在等于 4293588256。这到底是怎么回事? 另外,如果我在创建堆栈之前中断程序,GDB 会正确显示大小。
编辑: 我在 windows 8.1,东西的版本是:G++ 4.8.1; GDB 7.6.1
事实证明这确实是 GDB 7.6.1 或 G++ 4.8.1 的问题。 将 GDB 更新到 7.8.1 并将 G++ 更新到版本 4.9.2 解决了这个问题。