I want to examine the contents of a std::vector
in GDB, how do I do it? Let's say it's a std::vector<int>
for the sake of simplicity.
C++ – How to print the elements of a C++ vector in GDB
c++debugginggdbstlvector
Related Question
- C++ – The Definitive C++ Book Guide and List
- C++ – How to erase an element from std::vector<> by index
- C++ – the “–>” operator in C/C++
- C++ – the easiest way to initialize a std::vector with hardcoded elements
- C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming
- C++ – Why is the program slow when looping over exactly 8192 elements
- C++ – use a pointer rather than the object itself
Best Solution
With GCC 4.1.2, to print the whole of a std::vector<int> called myVector, do the following:
To print only the first N elements, do:
Explanation
This is probably heavily dependent on your compiler version, but for GCC 4.1.2, the pointer to the internal array is:
And the GDB command to print N elements of an array starting at pointer P is:
Or, in a short form (for a standard .gdbinit):