一个简单的 vector.push_back() 在我的代码中导致一些错误:
#include <vector>
using namespace std;
int main(int argc, const char *argv[])
{
vector<unsigned> stack;
stack.push_back(1);
stack.push_back(1); //stack.size() becomes 467369971 after this
stack.push_back(1);
stack.push_back(1);
... more push_back()s ...
return 0;
}
我正在使用 GDB 来检查它的行为……奇怪的是 stack.size() 在第二个 push_back() 之后出错了。变成467369971!可能有什么问题?我在 Win7 64 位上,我正在使用带有 G++ 4.7.0 的 MinGW
下面是 GDB 的输出:
(gdb) n
5 std::vector<unsigned> sta
(gdb) n
6 stack.push_back(1);
(gdb) display stack.size()
1: stack.size() = 0
(gdb) n
7 stack.push_back(1);
1: stack.size() = 1
(gdb)
8 stack.push_back(1);
1: stack.size() = 467369971 //goes wrong here
(gdb)
9 stack.push_back(1);
1: stack.size() = 467369971
(gdb)
10 stack.push_back(1);
1: stack.size() = 4 // gets "normal"
(gdb)
11 stack.push_back(1);
1: stack.size() = 467369971 // wrong again
(gdb)
12 stack.push_back(1);
1: stack.size() = 6
(gdb)
13 stack.push_back(1);
1: stack.size() = 7
(gdb)
14 stack.push_back(1);
1: stack.size() = 8
(gdb)
15 stack.push_back(1);
1: stack.size() = 467369971
(gdb)
16 stack.push_back(1);
1: stack.size() = 10
(gdb)
17 stack.push_back(1);
1: stack.size() = 11
(gdb)
18 stack.push_back(1);
1: stack.size() = 12
(gdb)
19 return 0;