I use Ubuntu 20.04-LTS with WSL(Windows Subsystem Linux), GDB version is 9.2, and I builded my c++ code with c++11.
I tried to access std::map's value with index in GDB, however GDB showed error message "Invalid cast".
My code is same for below
#include <iostream>
#include <map>
#include <string>
using std::cout;
using std::map;
using std::string;
using std::to_string;
int main()
{
map<string, int> si;
for(int i = 0; i < 10; ++i)
{
si[to_string(i)] = i;
}
for(int i = 0; i < 10; ++i)
{
cout << si[to_string(i)] << "\n";
}
}
And, in GDB
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ctest...
(gdb) l main
6 using std::map;
7 using std::string;
8 using std::to_string;
9
10 int main()
11 {
12 map<string, int> si;
13
14 for(int i = 0; i < 10; ++i)
15 {
(gdb) l 15
10 int main()
11 {
12 map<string, int> si;
13
14 for(int i = 0; i < 10; ++i)
15 {
16 si[to_string(i)] = i;
17 }
18
19 for(int i = 0; i < 10; ++i)
(gdb) b 18
Breakpoint 1 at 0x2579: file ctest.cpp, line 19.
(gdb) r
Starting program: /home/lksj/ctest
Breakpoint 1, main () at ctest.cpp:19
19 for(int i = 0; i < 10; ++i)
(gdb) p si["1"]
Invalid cast.
(gdb)
How can I access the std::map object value directly with index in GDB?