I have a small C++ program:
// test.cpp
#include <vector>
#include <iostream>
using namespace std;
template class vector<int>;
int main()
{
vector<int> v;
for (size_t i = 0; i < 10; ++i)
{
if (!v.empty())
cout << v[0] << endl; //<== Add a cond breakpoint here later
v.clear();
for (size_t j = 0; j < i; ++j)
v.push_back(i);
}
return 0;
}
This program is compiled in Ubuntu 14.04.3 LTS with gcc 4.8.4.
g++ -g -O0 -o test test.cpp
Start the program with gdb, and add a conditional breakpoint on line 13. The condition is "v.size() == 3 && v[0] == 3", as shown below. However, this condition doesn't work as expected. It stops when condition is not met.
(gdb) b 13 if v.size() == 3 && v[0] == 3
Breakpoint 1 at 0x400ca1: file test.cpp, line 13.
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400ca1 in main() at test.cpp:13
stop only if v.size() == 3 && v[0] == 3
Run the program and an error message is given:
...
Error in testing breakpoint condition:
Cannot access memory at address 0x0
Breakpoint 1, main () at test.cpp:13
13 cout << v[0] << endl;
And printing v.size()
shows it's 1, which is clearly not the condition that breakpoint should stop.
(gdb) p v.size()
$1 = 1
But if I replace the condition breakpoint with "v[0] == 3 && v.size() == 3" everything is fine.
(gdb) p v.size()
$2 = 3
What's the problem of the first condition?
[Update]
I upgraded gcc and gdb to 6.1.0 and 7.10 respectively on Ubuntu 14.04(both built from source), but this problem is still there.
$g++ -v
Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/6.1.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: ../gcc-6.1.0/configure --prefix=/usr --enable-languages=c,c++ --disable-multilib --disable-bootstrap --with-system-zlib Thread model: posix gcc version 6.1.0 (GCC)
$gdb -v
GNU gdb (GDB) 7.10
http://ftp.gnu.org/gnu/gdb/
. Rebuilt gdb. Unfortunately, I can observe the same problem. – Eric Z