CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2006
    Posts
    9

    using gdb to see into a c++ vector?

    i have a vector of floats, is there anyway to use gdb to see whats inside?

    Thanks

  2. #2
    Join Date
    Aug 2005
    Posts
    478

    Re: using gdb to see into a c++ vector?

    Windows XP, Visual Studio 2008, SVN

  3. #3
    Join Date
    Feb 2006
    Posts
    9

    Re: using gdb to see into a c++ vector?

    well, that sucks....
    thanks for the reply

  4. #4
    Join Date
    Aug 2005
    Posts
    478

    Re: using gdb to see into a c++ vector?

    Supposedly you can just evaluate expressions and print it, but it doesn't work for me.

    Code:
    GNU gdb 5.2.1
    Copyright 2002 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "i686-pc-mingw32"...
    (gdb) break *0x004013ef
    Breakpoint 1 at 0x4013ef: file t.cpp, line 9.
    (gdb) run
    Starting program: a.exe
    
    Breakpoint 1, 0x004013ef in main (argc=1, argv=0x3d3cd8) at t.cpp:9
    9               v.push_back(3);
    (gdb) p v[0]
    One of the arguments you tried to pass to operator[] could not be converted to w
    hat the function wants.
    (gdb)
    Windows XP, Visual Studio 2008, SVN

  5. #5
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: using gdb to see into a c++ vector?

    You have to know how the vector is implemented. If you are using the same gcc version I use, vector is a struct that contains an element _M_start which points to the first member.
    Code:
    (gdb) r
    Starting program: /home/treuss/src/vec2
    
    Breakpoint 1, main () at vec2.cc:5
    5         std::vector<float> v;
    (gdb) n
    6         v.push_back( 3.14 );
    (gdb) n
    7         v.push_back( 2.63 );
    (gdb) n
    8         exit( 1 );
    (gdb) p *v._M_start
    $1 = 3.1400001
    (gdb) p *(v._M_start+1)
    $2 = 2.63000011
    Hope this helps.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: using gdb to see into a c++ vector?

    As treusss mentioned, if you know how to get a pointer to the vector's internal buffer, then it's just a matter of going to the n'th element starting at the pointer to the buffer. Since a vector stores its values in contiguous memory, it is much simpler to view a vector's values then say, a std::list or std::map.

    Overloaded operators are not supported by most debuggers, which is why using v[0] didn't work. To see v[0], you must call a function to invoke [], and most debuggers do not support function calling.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jun 2009
    Posts
    3

    Post Re: using gdb to see into a c++ vector?

    This topic is quite old, but problem is still present. If somebody would like to run gdb under VisualStudio, then please check our addin WinGDB. Browsing of vectors will be much easier, than directly under gdb

    Kind regards,
    WinGDB team

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured