This is the mail archive of the
gdb@sourceware.org
mailing list for the GDB project.
Re: how to view content in stl vector
Am Montag, 18. Juni 2007 10:52:00 schrieb AlpÃr JÃttner:
> In a running On Fri, 2007-06-15 at 21:52 +0200, Mark Kettenis wrote:
> > > Date: Tue, 12 Jun 2007 11:05:21 -0400
> > > From: kdsfinger@gmail.com
> > >
> > > hi, all
> > > How may I view the content in a stl vector in ddd? (or the command to
> > > print it in gdb?). It seems I can only view the first element. I
> > > searched google but did not find answer. Thanks for help.
Hello
If you add this to your .gdbinit
<.gdbinit>
define dump_vector_simple
set $it = $arg0.data()
set $size = $arg0.size()
set $end = $it + $size
set $i = 0
while ($it != $end)
printf "[%u] == " , $i
output *($it)
printf "\n"
set $it++
set $i++
end
end
define dump_vector_as_virtual_array
set $it = $arg0.data()
set $size = $arg0.size()
output *$it@$size
printf "\n"
end
</.gdbinit>
the gdb output for this program
<test.cpp>
#include <vector>
int main(int argc, char **argv)
{
std::vector<double> vec;
for(int i = 0; i < 10; i++)
vec.push_back(i+1);
vec.data(); // dummy for gdb
return 0; // breakpoint here
}
</test.cpp>
will be
<gdb>
dump_vector_simple vec
[0] == 1
[1] == 2
[2] == 3
[3] == 4
[4] == 5
[5] == 6
[6] == 7
[7] == 8
[8] == 9
[9] == 10
</gdb>
or respectively
<gdb>
dump_vector_as_virtual_array vec
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
</gdb>
HTH, Maik Beckmann
PS: I'm using g++-4.1.2 and gdb-6.6