How to inspect elements in a C++ vector in gdb?

I have a vector of integers in a C++ program:

  1 #include <iostream>
  2 #include <vector>
  3 using std::vector;
  4 using std::cout;
  5 using std::endl;
  6
  7 int main()
  8 {
  9     vector<int> a;
 10     for (size_t i=0; i<7; ++i)
 11         a.push_back(i*2);
 12     cout << a.size() << endl;
 13     return 0;
 14 }

In GDB, when I break at line 12, I can inspect the value of a.size(). However, if I tried to inspect a[1], GDB complained "Could not find operator[]." If I tried to inspect a.at(1), GDB complained "Cannot evaluate function -- may be inlined". What should I do to inspect the content of the vector?



Read more here: https://stackoverflow.com/questions/68486078/how-to-inspect-elements-in-a-c-vector-in-gdb

Content Attribution

This content was originally published by angelaTIEN at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: