4

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;
nneonneo
  • 171,345
  • 36
  • 312
  • 383
neuron
  • 1,896
  • 1
  • 19
  • 24
  • 3
    Is `std::size()` actually 467369971? That is, if you added a print there, is that what you'd get? GDB has a tendency to be wrong especially if the code is optimized and you are calling member functions from the debugger. – nneonneo Sep 15 '12 at 03:55
  • @nneonneo well, you are right. printf seems to disagree with GDB indeed... but why would gdb be wrong? – neuron Sep 15 '12 at 04:03
  • What compilation settings did you use? What compiler version, GDB version, etc.? – nneonneo Sep 15 '12 at 04:09
  • I was using "g++ x.cpp -o x.exe -g", with GDB 7.4 – neuron Sep 15 '12 at 04:29
  • Try `-g3` (this might be just a cargo-cult thing). GCC 4.7 is pretty new; there might be corner cases that haven't been worked out between the GCC and GDB guys (try an older compiler if you have one). This is in all likelihood a reportable bug of GDB. If you're willing to put in the time, you may be able to figure out where GDB is getting that number by looking at the variables involved (`p stack`). – nneonneo Sep 15 '12 at 04:33

1 Answers1

4

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.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362