0

I've been trying to debug a program that uses several STL vectors, and it seems that GDB (7.6.1) is incorrectly displaying the vectors' sizes (i.e. number of elements, not capacity). Needless to say, this is creating major difficulties in debugging my code. I've created a simple piece of code that replicates the problem, vector_test.cpp. Running Windows 64 bit.

#include <vector>
#include <iostream>

int main() {
    std::vector<int> v;

    v.push_back(4);
    std::cout << "v.size() = "<<v.size()<<"\n";
    v.push_back(8);
    std::cout << "v.size() = "<<v.size()<<"\n";
    v.push_back(15);
    std::cout << "v.size() = "<<v.size()<<"\n";
    v.push_back(16);
    std::cout << "v.size() = "<<v.size()<<"\n";
    v.push_back(23);
    std::cout << "v.size() = "<<v.size()<<"\n";
    v.push_back(42);
    std::cout << "v.size() = "<<v.size()<<"\n";

    return 0;
}

After compiling with g++ -Wall -g -O0 -o vector_test.exe vector_test.cpp and running vector_test.exe, the output is (naturally)

v.size() = 1
v.size() = 2
v.size() = 3
v.size() = 4
v.size() = 5
v.size() = 6

But debugging in GDB tells a very different story... v.size() seems to be evaluated completely incorrectly!

(gdb) break vector_test.cpp:5
Breakpoint 1 at 0x401477: file vector_test.cpp, line 5.
(gdb) run
Starting program: C:\Users\Robin Armstrong\Desktop/vector_test.exe
[New Thread 3384.0x1b64]
[New Thread 3384.0x2fa0]
[New Thread 3384.0x300c]
[New Thread 3384.0x1b10]

Breakpoint 1, main () at vector_test.cpp:5
5           std::vector<int> v;
(gdb) disp v.size()
1: v.size() = 1956771
(gdb) next
7           v.push_back(4);
1: v.size() = 0
(gdb)
8           std::cout << "v.size() = "<<v.size()<<"\n";
1: v.size() = 1
(gdb)
v.size() = 1
9           v.push_back(8);
1: v.size() = 3825453421
(gdb)
10          std::cout << "v.size() = "<<v.size()<<"\n";
1: v.size() = 412859506
(gdb)
v.size() = 2
11          v.push_back(15);
1: v.size() = 3825453421
(gdb)
12          std::cout << "v.size() = "<<v.size()<<"\n";
1: v.size() = 412859506
(gdb)
v.size() = 3
13          v.push_back(16);
1: v.size() = 3825453421
(gdb)
14          std::cout << "v.size() = "<<v.size()<<"\n";
1: v.size() = 4
(gdb)
v.size() = 4
15          v.push_back(23);
1: v.size() = 3825453421
(gdb)
16          std::cout << "v.size() = "<<v.size()<<"\n";
1: v.size() = 412859506
(gdb)
v.size() = 5
17          v.push_back(42);
1: v.size() = 3825453421
(gdb)
18          std::cout << "v.size() = "<<v.size()<<"\n";
1: v.size() = 6
(gdb)
v.size() = 6
20          return 0;
1: v.size() = 3825453421

I'm completely at a loss for how to explain this, and I can't find a description of a similar problem elsewhere. Any thoughts?

  • 1
    Exact dupe but it doesn't really have any useful information except that something in your tool chain is broken (GDB or the compiler):https://stackoverflow.com/questions/12434364/gdb-wrong-values-for-vector-size – NathanOliver Jul 09 '19 at 18:47
  • FWIW, just did a quick check and I'm not seeing this problem (seeing the expected behavior instead) on gdb 8.1 w/ g++ 7.4.0 on Ubuntu. – Emmet Jul 09 '19 at 18:57
  • Have you installed the scripts to enable pretty printing for stl objects? If so, what is the result if you display v instead? Typically gdb will be able to print all members like size, capacity and the members in the vector. Did this work? – Klaus Jul 09 '19 at 19:05
  • Thanks for the input, everyone. I'm trying to reinstall GDB on the theory that the debugger itself is broken. Hopefully it works! – FlyingPiper Jul 10 '19 at 19:57
  • @Klaus To follow up on this comment, I did install the scripts for pretty printing. It worked just fine and displayed the size correctly. – FlyingPiper Jul 30 '19 at 16:03

0 Answers0