Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c++ - GDB wrong values for vector.size()

A simple vector.push_back() causes some error in my code:

#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;
}

I'm using GDB to check its behavior... and the weird thing is that stack.size() goes wrong after the second push_back(). It becomes 467369971! What may be wrong? I'm on Win7 64-bit, and I'm using MinGW with G++ 4.7.0

Below is the output of 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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What may be wrong?

It's a bug in your compiler, or in your GDB. It does not reproduce on Linux using g++ (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3 and GDB 7.4:

(gdb) n
7       stack.push_back(1);
1: stack.size() = 0
(gdb) 
8       stack.push_back(1); //stack.size() becomes 467369971 after this
1: stack.size() = 1
(gdb) 
9       stack.push_back(1);
1: stack.size() = 2
(gdb) 
10      stack.push_back(1);
1: stack.size() = 3
(gdb) 
11      stack.push_back(1);
1: stack.size() = 4
(gdb) 
12      stack.push_back(1); //stack.size() becomes 467369971 after this
1: stack.size() = 5
(gdb) 
13      stack.push_back(1);
1: stack.size() = 6
(gdb) 
14      stack.push_back(1);
1: stack.size() = 7
(gdb) 
15      return 0;
1: stack.size() = 8
(gdb) 
16  }
(gdb) q

Unfortunately, figuring out which tool is to blame here will be somewhat hard: you'll need to examine the debuginfo generated. Instead you may try to reproduce the problem with different versions of GCC and GDB. If varying GCC causes the bug to disappear, it's probably a bug in GCC. If varying GDB version makes the bug disappear, it's probably a GDB bug.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...