I am experiencing what seems to me to be unusual behavior with the STL
vector class in gdb.

In the code below, when v is passed by const reference in the Bar
example, I can access the values of the vector with the [] operator
under gdb.  When I pass by value in the Foo example, I cannot seem to
access the value in gdb.

The code below illustrates the behavior.  It was compiled with
gcc-2.95.2 with the cmomand 'g++ -g test.cpp'.  The gdb 5.0 session is
listed below the code.


---------- sample code ---------------
#include <iostream>
#include <vector>

class Foo {
public:
  float operator()(vector<float> v) {
    size_t N = v.size();
    float ret(0.0);
    for (size_t i = 0; i < N; ++i)
      ret += v[i];
    return ret;
  }
};

class Bar {
public:
  float operator()(const vector<float>& v) {
    size_t N = v.size();
    float ret(0.0);
    for (size_t i = 0; i < N; ++i)
      ret += v[i];
    return ret;
  }
};



int main () {
  size_t N = 10;
  vector<float> x(N);
  for (size_t i = 0; i < N; ++i)
    x[i] = 2.0*i;

  Foo f;
  cout << "The sum of x is " << f(x) << endl;
  Bar b;
  cout << "The sum of x is " << b(x) << endl;
}



----------- gdb session ---
Current directory is ~/c/examples/
GNU gdb 5.0
This GDB was configured as "i686-pc-linux-gnu"...
(gdb) b 'Foo::operator()(vector<float, allocator<float> >)' 
Breakpoint 1 at 0x8052d0e: file test.cpp, line 7.
(gdb) b 'Bar::operator()(vector<float, allocator<float> > const &)' 
Breakpoint 2 at 0x8052dbe: file test.cpp, line 18.
(gdb) run
Starting program: /net/home/guest/jdhunter/c/examples/a.out 

Breakpoint 1, Foo::operator() (this=0xbffff337, 
    v={<_Vector_base<float,allocator<float> >> = 
{<_Vector_alloc_base<float,allocator<float>,>> = {_M_start = 0xbffff328, _M_finish = 
0x4008d40a, 
          _M_end_of_storage = 0xbffff358}, <No data fields>}, <No data fields>}) at 
test.cpp:7
(gdb) p v[0]
$1 = {<_Vector_base<float,allocator<float> >> = 
{<_Vector_alloc_base<float,allocator<float>,>> = {_M_start = 0x805a378, _M_finish = 
0x805a3a0, 
      _M_end_of_storage = 0x805a3a0}, <No data fields>}, <No data fields>}
(gdb) c
Continuing.
The sum of x is 90

Breakpoint 2, Bar::operator() (this=0xbffff327, v=@0xbffff348) at test.cpp:18
(gdb) p v[0]
$2 = (float &) @0x805a350: 0
(gdb) 



Incidentally, in designing this example, I was trying to replicate
some strange (to me) behavior I found while debugging a program too
complex to make a meaningful post; unfortunately, I got different
behavior in my example than in my real code.  In that code, when I
used the [] operator to access the contents of a vector<double>
passed by const reference to a function with this signature:

SenSynapse::compute_everything(const vec_t& pars, const vec_t& t)

I got the mystifying response:

(gdb) p t[inow]
cannot subscript something of type `vector<double,allocator<double> >'

Same thing trying to access pars with the [] operator (the function
does use the [] operator, so it presumably has been included by the
compiler)


Thanks,
John Hunter

Reply via email to